all groups > sql server mseq > august 2004 >
You're in the

sql server mseq

group:

SQL Question



SQL Question anonymous NO[at]SPAM discussions.microsoft.com
8/23/2004 8:09:47 AM
sql server mseq: Hi, everyone:

I was trying to create a tempory table and then join it
with other existing tables to do some query.
I know in DB2 SQL you can do the following:

WITH temp (var1 var2) as
(
select field1, field2
from table1
)

select
tb1.field2,
tb2.var2


from
temp as tb1,
table2 as tb2

where
tb1.var1 = tb2.field1


How do I do this in SQL server ?

Thanks!!

F.S.

Re: SQL Question Vishal Parkar
8/23/2004 8:55:04 PM
[quoted text, click to view]
WITH temp (var1 var2) as
(
select field1, field2
from table1
)
<<

above syntax's equivalent in sql server is:

select field1 as var1,field2 as var2 into temp from table1

--the query remain the same :
select tb1.field2, tb2.var2
from
temp as tb1,
table2 as tb2
where
tb1.var1 = tb2.field1

--using ansi syntax you can have query as:

select tb1.var2, tb2.var2
from temp as tb1 join table2 as tb2
on tb1.var1 = tb2.field1

--without creating the temporary table you can make use of SELECT statement
as an inline view as follows:

select tb1.field2, tb2.var2
from (select field1,field2 from table1) as tb1 join table2 as tb2
on tb1.field1 = tb2.field1


--
Vishal Parkar
vgparkar@yahoo.co.in | vgparkar@hotmail.com

Re: SQL Question anonymous NO[at]SPAM discussions.microsoft.com
9/4/2004 11:16:53 AM
Thanks a lot!!!

[quoted text, click to view]
AddThis Social Bookmark Button