Solved

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

Posted on 2003-11-21
4
2,017 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 125 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:
bhess1 earned 125 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

776 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