![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hi everybody! I have several tables from which I want to exract the SAME value (along with other referenced data). All the values are in the same column within the tables. How can I achieve this? TIA. Andro ************************* example: (piping bill of material where tables represent piping systems) TBL1-water pos. item type size rating --------------------------------- 01 flange 3" 300# 02 valve wafer 2" 150# 03 valve ball 1" 150# 04 elr90 2" TBL2-oil (similar like above).........etc. ---------------------------------- how to select (say) BALL VALVES from these tables along with pos.,size, rating etc.? |
#3
| |||
| |||
|
|
On Mon, 24 Apr 2006 00:04:40 +0200, andro wrote: Hi everybody! I have several tables from which I want to exract the SAME value (along with other referenced data). All the values are in the same column within the tables. How can I achieve this? TIA. Andro ************************* example: (piping bill of material where tables represent piping systems) TBL1-water pos. item type size rating --------------------------------- 01 flange 3" 300# 02 valve wafer 2" 150# 03 valve ball 1" 150# 04 elr90 2" TBL2-oil (similar like above).........etc. ---------------------------------- how to select (say) BALL VALVES from these tables along with pos.,size, rating etc.? Hi Andro, SELECT 'water' AS system, pos, item, type, size, rating FROM TBL1_water WHERE type = 'ball' UNION ALL SELECT 'oil' AS system, pos, item, type, size, rating FROM TBL2_oil WHERE type = 'ball' UNION ALL etc But a better solution would be to use just one table, with water/oil/etc as extra column (part of the primary key), instead of splitting the data over several similar tables. -- Hugo Kornelis, SQL Server MVP |
#4
| |||
| |||
|
|
Hugo ! Thank you very much for your help. (I've noticed that such a simple question is not so simple to resolve with basic SQL knowledge such as mine). It is difficult to use first approach by entering everytime TBL_NAME (system here). |
|
Is there any easier way - by using table names as "parameters"? |
|
I like your second approach. Would you be more specific about "extra column (***part of the primary key***)" What do you mean by "part of the primary key"? |
#5
| |||
| |||
|
|
On Mon, 24 Apr 2006 00:46:02 +0200, andro wrote: Hugo ! Thank you very much for your help. (I've noticed that such a simple question is not so simple to resolve with basic SQL knowledge such as mine). It is difficult to use first approach by entering everytime TBL_NAME (system here). Hi Andro, I'm sorry, I don't understand this part of your message. Is there any easier way - by using table names as "parameters"? There is, but it's not recommended, because of the security implications. If you want to read about the method *and* the risks it has, go to http://www.sommarskog.se/dynamic_sql.html. I like your second approach. Would you be more specific about "extra column (***part of the primary key***)" What do you mean by "part of the primary key"? That would be easier to answer if you had told me exactly how yoour tables currently look. I'll use a made-up example to illustrate this. Consider the following (bad!) design for daily sales data: CREATE TABLE North_Sales (SaleDate smalldatetime NOT NULL, ProductNo int NOT NULL, AmountSold int NOT NULL, PRIMARY KEY (SaleDate, ProductNo), FOREIGN KEY (ProductNo) REFERENCES Products ) CREATE TABLE East_Sales (SaleDate smalldatetime NOT NULL, ProductNo int NOT NULL, AmountSold int NOT NULL, PRIMARY KEY (SaleDate, ProductNo), FOREIGN KEY (ProductNo) REFERENCES Products ) (and two more for regions South and West) These four tables can and should be replaced by this single table: CREATE TABLE Sales (SaleDate smalldatetime NOT NULL, Region char(5) NOT NULL, ProductNo int NOT NULL, AmountSold int NOT NULL, PRIMARY KEY (SaleDate, Region, ProductNo), FOREIGN KEY (ProductNo) REFERENCES Products, CHECK (Region IN ('North', 'South', 'East', 'West')) ) Notice how I added a column "Region", _AND* added this column to the list of columns that make up the primary key. (I also added a check constraint - this should be replaced by a FOREIGN KEY constraint if there's a Regions table in the database as well). -- Hugo Kornelis, SQL Server MVP |
#6
| |||
| |||
|
|
That would be easier to answer if you had told me exactly how yoour tables currently look. |
#7
| |||
| |||
|
|
.. repeat the qurey within the tables using "parameter" TBL_NAME where neccessary. |
#8
| ||||
| ||||
|
|
That would be easier to answer if you had told me exactly how yoour tables currently look. Tables look like this: |
|
TBL1-water pos. item type size rating operation pcs. material identification_no ( <-----unique ! ) |
|
TBL1-water pos. item type size rating operation pcs. material identification_no ( <-----unique ! ) ----------------------------------------------------------------------------------- 01 flange 3" 300# NULL 2 AISI316L 012324585 02 valve wafer 2" 150# hydraulic 3 CS/SS 065898329 03 valve ball 1" 150# manual 1 BZ/BZ 378987548 04 elr90 2" NULL 8 CS 879539287 05 etc...... |
|
TBL2-oil pos. item type size rating operation pcs. material identification_no ( <-----unique ! ) ----------------------------------------------------------------------------------- 01 foo bar 2" 200# automatic 7 KG/H33C 477396411 02 flange wafer 2" 250# electric 3 CS/SS 378987548 03 etc...... |
#9
| |||
| |||
|
|
Regarding the first part.....nothing special - it's just that I'm kinda bored to repeatedly enter nearly the same query for every system here. (There are not just several but say 40-50 which are changing from project to project.) I just wondered (please note: I'm a newbie) is there a way to instruct the system in a kind of "loop" DO - UNTIL or FOR/NEXT for every system table in database to repat the qurey within the tables using "parameter" TBL_NAME where neccessary. Some kind of automation maybe - you know. (Seems very advanced request). |
|
(I heaven't checked your link yet ). |
#10
| |||
| |||
|
![]() |
| Thread Tools | |
| Display Modes | |
| |