Store in variables without cursors | SQL Server Performance Forums

SQL Server Performance Forum – Threads Archive

Store in variables without cursors

Hi, I’m new to SQL Server and have been trying to store the count of total number of records in a table into a variable without using cursors. Could any one help whats wrong the procedure below as i dont get the output of the ‘PRINT’ statement. CREATE PROCEDURE test_store1 AS
DECLARE @ct INT
SELECT @ct = COUNT(Rep_Id)FROM FFN_REP
— Make sure the table has data.
IF ISNULL(@ct,0) = 0
BEGIN
SELECT ‘No data in found in table!’
RETURN
END
PRINT @ct

What’s up sdivvela, DECLARE @ct INT
SET @ct =(SELECT COUNT(Rep_Id) FROM FFN_REP) –MADE CHANGE HERE
— Make sure the table has data.
IF ISNULL(@ct,0) = 0
BEGIN
SELECT ‘No data in found in table!’
RETURN
END
PRINT @ct
Wait a minute, Lazy_DBA, the previous syntax was correct and it should work. I see the implementation like this-
CREATE PROCEDURE test_store1 AS
DECLARE @ct INT
SELECT @ct = COUNT(Rep_Id)FROM FFN_REP
— Make sure the table has data.
IF (@ct = 0)
BEGIN
SELECT ‘No data in found in table!’
RETURN
END
PRINT @ct Gaurav
Moderator
Man thrives, oddly enough, only in the presence of a challenging environment- L. Ron Hubbard

The views expressed here are those of the author and no one else. There are no warranties as to the reliability or accuracy of anything presented here.
Your right! I guess his problem was already solved huh.
]]>