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
 Required tablecount

Author  Topic 

kneel
Starting Member

36 Posts

Posted - 2008-03-25 : 09:35:12
Hi all,

I have following stored procedure. After executing this

SET @STRSQL = 'SELECT @TABLECOUNT = COUNT(1) FROM [' + @DATABASE1+ ']..CUSTOMERS'
EXEC (@STRSQL)
PRINT @TABLECOUNT


How can I get total count in @TABLECOUNT variable. I am facing error as Must declare variable @TABLECOUNT. Though I have declared it.

Does anyone knows work around for this. Thanks in advance.

--kneel

PeterNeo
Constraint Violating Yak Guru

357 Posts

Posted - 2008-03-25 : 10:14:56
try this

declare @TABLECOUNT int,
@SqlStr nvarchar(max),
@DATABASE1 varchar(100)

select @SqlStr = '',
@DATABASE1 = ''

select @SqlStr = 'SELECT @TABLECOUNT = COUNT(1) FROM [' + @DATABASE1+ ']..CUSTOMERS'

Exec sp_executesql @SqlStr, N'@TABLECOUNT int output', @TABLECOUNT=@TABLECOUNT OUTPUT
Print @TABLECOUNT



Go to Top of Page

kneel
Starting Member

36 Posts

Posted - 2008-03-25 : 10:56:54
Thanks a lot. It worked.

--kneel
Go to Top of Page
   

- Advertisement -