Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Creating a table from multiple existing tables

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-09-17 : 13:03:16
Naveen writes "I need to retrieve data from 4 different tables and store the results in a newly created table. I need to be able to match against a column which exists in all 4 tables but the records are not in the same order. Is it also possible to define the column names to export data to during this process? PLEASE HELP"

DavidD
Yak Posting Veteran

73 Posts

Posted - 2002-09-17 : 20:19:53
Naveen,

Your questions a bit short on detail, but is this what you are after:

insert into newtable (val1, val2, val3, val4...)
select a.val1, b.val2, b.val3...
from table1 a join table2 b on a.key = b.key join table3 c on b.key = c.key ....

Send more detail and we can help more.
Regards
David

Go to Top of Page

Crespo

85 Posts

Posted - 2002-09-18 : 03:58:36
quote:

Naveen writes "I need to retrieve data from 4 different tables and store the results in a newly created table. I need to be able to match against a column which exists in all 4 tables but the records are not in the same order. Is it also possible to define the column names to export data to during this process? PLEASE HELP"



Could you please be more specific with your question?
Some exmaples might also help. I think what you're asking for is quite straightforward.

Good Luck!

Crespo.
Hewitt Bacon & Woodrow
Epsom
Surrey
United Kingdom
Go to Top of Page

ytrenty
Starting Member

2 Posts

Posted - 2002-09-18 : 23:58:08
Naveen -
Because you have given us so little detail I can only speculate as to what you are asking. My interpretation is that you need to create a temp table on the fly from the 4 different tables, and you would like to be able to define aliases on the columns for the original tables when you create the new one.

If that is what you want to do, then you want to use a SELECT INTO statement.

If you have 4 tables, t1, t2, t3, and t4, each with a common id field for joining, then it might look something like this:

SELECT t1.field AS t1_field,
t2.field AS t2_field,
t3.field AS t3_field,
t4.field AS t4_field
INTO #temp_table_name
FROM t1
INNER JOIN t2
ON t1.id = t2.id
INNER JOIN t3
ON t2.id = t3.id
INNER JOIN t4
ON t3.id = t4.id

If this is your problem, hope this helps. If not, give us more info and let's see if we can help you out.

Good Luck!

Trent

Go to Top of Page
   

- Advertisement -