Saving a select count(*) value to an integer (SQL Server)

Posted by larryq on Stack Overflow See other posts from Stack Overflow or by larryq
Published on 2010-03-08T16:55:38Z Indexed on 2010/03/09 0:21 UTC
Read the original article Hit count: 550

Filed under:
|
|
|

Hi everyone,

I'm having some trouble with this statement, owing no doubt to my ignorance of what is returned from this select statement:

declare @myInt as INT
set @myInt = (select COUNT(*) from myTable as count)

if(@myInt <> 0) 
begin
   print 'there's something in the table'
end

There are records in myTable, but when I run the code above the print statement is never run. Further checks show that myInt is in fact zero after the assignment above. I'm sure I'm missing something, but I assumed that a select count would return a scalar that I could use above?

© Stack Overflow or respective owner

Related posts about sql-server

Related posts about aggregate