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
 General SQL Server Forums
 New to SQL Server Programming
 SELECT from multible tables

Author  Topic 

sqlneve
Starting Member

16 Posts

Posted - 2009-01-21 : 13:03:01
I want to select all records from 3 different tables and then export out all 3 as one. Below is what I thought might work. But this didn't get me very far. Can anyone hep? Would this be an insert into query? Thanks!

SELECT *
FROM sdeallsites.dbo.arall1
AND sdeallsites.dbo.arall
AND sdeallsites.dbo.arsw

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-21 : 13:06:08
[code]
SELECT *
FROM sdeallsites.dbo.arall1 t1
JOIN sdeallsites.dbo.arall t2
ON t2.field=t1.field
AND sdeallsites.dbo.arsw t3
ON t3.field={t1/t2}.field
[/code]

fields represent column by which tables are related
Go to Top of Page

askben
Starting Member

8 Posts

Posted - 2009-01-21 : 18:23:30
SELECT *
FROM sdeallsites.dbo.arall1
UNION ALL
SELECT *
FROM sdeallsites.dbo.arall
UNION ALL
SELECT *
FROM sdeallsites.dbo.arsw

This will union all the tables together into one rowset to return.

------------
Ben Miller
Go to Top of Page

TechnoConnect Team
Starting Member

1 Post

Posted - 2009-01-22 : 00:39:07
SELECT Col1, Col2, Col3
FROM sdeallsites.dbo.arall1
UNION ALL
SELECT Col1, Col2, Col3
FROM sdeallsites.dbo.arall
UNION ALL
SELECT Col1, Col2, Col3
FROM sdeallsites.dbo.arsw

Just to add to Ben Miller's Reply, pls follow the syntax given above to includ the column names in your select statement, this will improve the performance of the SELECT statement. Also make sure that Col1, Col2, Col3 etc in all three SELECT statements appear in the same sequence (irrespective of sequence in table) and have the same data types.

Hope this helps!

| TechnoConnect Team | http://www.techno-connect.blogspot.com |
| This info is provided "AS IS" with no warranties & confers no rights. |
Go to Top of Page
   

- Advertisement -