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 2005 Forums
 Transact-SQL (2005)
 Why does this query does not work?

Author  Topic 

tamancha.1
Starting Member

37 Posts

Posted - 2010-04-27 : 14:18:02
(select distinct(user_id)
from #work_4_un
where mycount=1)
intersect
(select distinct(user_id)
from #work_4_un
where mycount=2)
into #test

Error: Msg 156, Level 15, State 1, Line 8
Incorrect syntax near the keyword 'into'.

But this works, why?
(select distinct(user_id)
from #work_4_un
where mycount=1)
intersect
(select distinct(user_id)
from #work_4_un
where mycount=2)

I want to store the results into table.

hanbingl
Aged Yak Warrior

652 Posts

Posted - 2010-04-27 : 14:26:28
[code]select * into #test from (select distinct(user_id)
from #work_4_un
where mycount=1
intersect
select distinct(user_id)
from #work_4_un
where mycount=2)t[/code]
Go to Top of Page

tamancha.1
Starting Member

37 Posts

Posted - 2010-04-27 : 14:37:09
Thanks, Why do we have a t here in the end.Why it does not work without a t,May be sQL statement cant end with )
Go to Top of Page

hanbingl
Aged Yak Warrior

652 Posts

Posted - 2010-04-27 : 14:43:55
You have to give it an alias. you can use AS TABLE1 as long as there's an alias.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-28 : 05:54:37
quote:
Originally posted by tamancha.1

Thanks, Why do we have a t here in the end.Why it does not work without a t,May be sQL statement cant end with )


t is name given for table you created on the fly in query

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -