Click here to Skip to main content
15,880,543 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a dynamic query (that works) in a stored procedure. It gets N records starting at record number X from a larger set of records. I now need to return the count of the larger set of records to the calling application, and I don't want to have to create another stored proc to do it.

I defined the OUT parameter in the store proc's prototype, but I haven't been able to find a way to get the count into the parameter. It keeps telling me I have to define the scalar parameter @totalRecords, but it IS already defined.

My query looks something like this"

The stored procedure looks something like this:

SQL
sp_mySP( parameters..., @totalRecords int OUT)
as
begin
    -- the query string essentially looks like this (comments added for clarity):

    set @queryStr = 
    -- drop temptable if it exists
    + 'if #temptable exists drop #temptable; '

    -- gets the data specified by parameters
    + 'select * into #temptable from mytable where blah blah; '

    -- dets the data in the specified rows
    + 'select * from #temptable where blah blah; '

    -- counts the records in #temptable and stores them in out parameter
    + ' set @totalRecords = (select count(*) from #temptable); '

    -- at this point, I call 
    exec (@queryStr)
end


How do I set @totalRecords inside a dynamic query?

EDIT ===============

I've also tried this:

Change the last part of the string to this:

SQL
+ ' set @outCount = (select count(*) from #temptable); '


and then did this:

SQL
declare @outCounter int
exec sp_executesql @queryStr, N'@outCount int OUTPUT', @outCounter OUPUT
select @outCounter as counter


This results in outCounter being set to NULL
Posted
Updated 2-Jun-11 5:47am
v4

I ended up doing this:


SQL
sp_mySP( parameters..., @totalRecords int OUT)
as
begin
    -- the query string essentially looks like this (comments added for clarity):

    set @queryStr = 
    -- drop temptable if it exists
    + 'if #temptable exists drop #temptable; '
 
    -- gets the data specified by parameters
    + 'select * into #temptable from mytable where blah blah; '
 
    -- dets the data in the specified rows
    + 'select * from #temptable where blah blah; '
 
    -- counts the records in #temptable and stores them in out parameter
    -- I changed the variable here from the sp's out parameter to @outCount <<--------
    + ' set @outCount = (select count(*) from #temptable); '
 
    -- at this point, I call 
    declare @outCounter Int

    -- and change this line to be this: <<--------------
    execute (sp_executesql @queryStr, N'outCount int OUTPUT', @outCounter OUTPUT)

    -- and finally, I did this: <<--------------
    set @totalrecords = @outCounter
end
 
Share this answer
 
v2
Comments
TweakBird 2-Jun-11 14:31pm    
Good Answer. My 5!
i tried like this it worked for me


SQL
declare @cahr int
exec('select '+@cahr  + ' count(*) from dbo.Users')
 
Share this answer
 
This article I have found to be a good reference. The example below is taken from it

Dynamic SQL[^]


SQL
SELECT @sql =
  N' SELECT @cnt = COUNT(*) FROM dbo.' + quotename(@tbl) +
  N' WHERE LastUpdated BETWEEN @fromdate AND ' +
  N'                           coalesce(@todate, ''99991231'')'
  SELECT @params = N'@fromdate datetime, ' +
                   N'@todate   datetime = NULL, ' +
                   N'@cnt      int      OUTPUT'
  EXEC sp_executesql @sql, @params, '20060101', @cnt = @count OUTPUT
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900