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
 Why doesn't this parse?

Author  Topic 

boggyboy
Yak Posting Veteran

57 Posts

Posted - 2012-11-01 : 12:21:19
Getting a syntax error for the following in SQL 2008. Suggestions?

Declare @Count int
set @Count =
(
select count(*)
from
(select distinct [item] from data where [item] is not null)
)

Nick W Saban

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-11-01 : 12:23:25
What is the error? My guess is that you need to explicitly name/alias your derived table. Something like:
Declare @Count int
set @Count =
(
select count(*)
from
(select distinct [item] from data where [item] is not null) AS t
)
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-11-01 : 12:27:54
Just out of curiosity, what's wrong with:
DECLARE @Count INT
SELECT @Count = COUNT(DISTINCT item) FROM DATA WHERE [item] IS NOT NULL
Go to Top of Page

boggyboy
Yak Posting Veteran

57 Posts

Posted - 2012-11-01 : 13:17:47
Thanks to both of you!

Nick W Saban
Go to Top of Page
   

- Advertisement -