[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2003-11-21
4
Medium Priority
?
2,031 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
  • 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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

873 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