?
Solved

MSSQL---Using Execute & Dynamic SQL Statements to return a COUNT to a variable

Posted on 2003-11-21
4
Medium Priority
?
2,026 Views
Last Modified: 2011-04-14
My SQL Statement below is dynamic based on server name and db name and the servers i'm using are linked----however, I can't figure out how to get the Count from my select statement. My Rowcount is always coming back as 1, when it should be zero in some cases.
I've tried:

Code:--------------------------------------------------------------------------------
if ((@SERV_NAME != '') and (@DB_NAME != ''))
        begin
            set @strSQL = 'select count(*)from ' + ltrim(rtrim(@SERV_NAME)) + '.' + ltrim(rtrim(@DB_NAME)) + '.dbo.sysobjects where name = ''images'''
            execute(@strSQL)
            set @tmp_rc = @@ROWCOUNT
--------------------------------------------------------------------------------
and
Code:--------------------------------------------------------------------------------
declare @myCount int
set @strSQL = 'select @myCount = count.....
execute (@strsql)
--------------------------------------------------------------------------------Neither one works right. Help please---what am i missing

Obviously, select @myCount = count..... works fine if it's not dynamic (ie, in a varchar variable), but I can't figure out how
0
Comment
Question by:calpha
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 500 total points
ID: 9800940
You need to use the sp_executesql extended stored procedure to return the count.  Checkout this article from MSDN:
INF: Using Output Parameters with sp_executesql
http://support.microsoft.com/default.aspx?scid=kb;en-us;262499

Anthony
0
 
LVL 32

Accepted Solution

by:
Brendt Hess earned 500 total points
ID: 9801085
YOu can do this with the sp_executeSQL Stored Procedure, e.g.:

if ((@SERV_NAME != '') and (@DB_NAME != ''))
        begin
            set @strSQL = 'select @Ct=count(*)from ' + ltrim(rtrim(@SERV_NAME)) + '.' + ltrim(rtrim(@DB_NAME)) + '.dbo.sysobjects where name = ''images'''
            execute sp_executeSQL @strSQL, N'@Ct int output', @Ct = @tmp_rc OUTPUT


After this, your @tmp_rc variable should have the count.
0
 

Author Comment

by:calpha
ID: 9801178
Thanks Guys.  I felt bad just giving it to one since both of you had the right answer.

Even though you were dead on acperkins, I felt i had to split points because bhess1 gave me the exact code I needed to use.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 9801248
That is very fair.  I appreciate the explanation all the same, not many do that here.

Thanks,
Anthony
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question