140

Executing dynamic SQL as follows in Stored Procedure:

DECLARE @sqlCommand nvarchar(1000)
DECLARE @city varchar(75)
SET @city = 'London'
SET @sqlCommand = 'SELECT COUNT(*) FROM customers WHERE City = @city'
EXECUTE sp_executesql @sqlCommand, N'@city nvarchar(75)', @city = @city

How do I use the count(*) column value as return value in the SP?

Robert Lujo
  • 15,383
  • 5
  • 56
  • 73
Peter Lindholm
  • 2,420
  • 5
  • 23
  • 27

6 Answers6

252
DECLARE @sqlCommand NVARCHAR(1000)
DECLARE @count INT
DECLARE @city VARCHAR(75)
SET @city = 'New York'

SET @sqlCommand = 'SELECT @cnt=COUNT(*) FROM customers WHERE City = @city'
EXECUTE sp_executesql @sqlCommand, N'@city nvarchar(75), @cnt int OUTPUT', @city = @city, @cnt = @count OUTPUT

SELECT @count
Andrew
  • 18,680
  • 13
  • 103
  • 118
Sage
  • 4,769
  • 1
  • 21
  • 28
  • 4
    +1: You beat me to it, need to declare a variable, and mark it as an OUTPUT. [For more info, and a recommended read for SQL Server dynamic SQL, see The curse and blessings of dynamic SQL](http://www.sommarskog.se/dynamic_sql.html#sp_executesql) – OMG Ponies Oct 01 '10 at 15:46
  • 1
    Thank you. The OUTPUT keyword in N'@city nvarchar(75),@cnt int OUTPUT' was what I was missing. – Peter Lindholm Oct 01 '10 at 17:28
  • 1
    Is there no solution that doesn't require adding an output variable to the dynamic statement??? – Tab Alleman Dec 20 '16 at 16:18
5

dynamic version

    ALTER PROCEDURE [dbo].[ReseedTableIdentityCol](@p_table varchar(max))-- RETURNS int
    AS
    BEGIN
        -- Declare the return variable here
       DECLARE @sqlCommand nvarchar(1000)
       DECLARE @maxVal INT
       set @sqlCommand = 'SELECT @maxVal = ISNULL(max(ID),0)+1 from '+@p_table
       EXECUTE sp_executesql @sqlCommand, N'@maxVal int OUTPUT',@maxVal=@maxVal OUTPUT
       DBCC CHECKIDENT(@p_table, RESEED, @maxVal)
    END


exec dbo.ReseedTableIdentityCol @p_table='Junk'
Ab Bennett
  • 1,391
  • 17
  • 24
2

You've probably tried this, but are your specifications such that you can do this?

DECLARE @city varchar(75)
DECLARE @count INT
SET @city = 'London'
SELECT @count = COUNT(*) FROM customers WHERE City = @city
Brad
  • 15,361
  • 6
  • 36
  • 57
0
DECLARE @sqlCommand nvarchar(1000)
DECLARE @city varchar(75)
DECLARE @cnt int
SET @city = 'London'
SET @sqlCommand = 'SELECT @cnt=COUNT(*) FROM customers WHERE City = @city'
EXECUTE sp_executesql @sqlCommand, N'@city nvarchar(75)', @city = @city
RETURN @cnt
CristiC
  • 22,068
  • 12
  • 57
  • 89
  • 1
    I think your answer got cut off. – Sage Oct 01 '10 at 15:44
  • Msg 137, `Must declare the scalar variable "@cnt".` Msg 178, `A RETURN statement with a return value cannot be used in this context.`. A nice piece of work, bro )) – it3xl Aug 19 '18 at 13:34
-1

this could be a solution?

declare @step2cmd nvarchar(200)
DECLARE @rcount NUMERIC(18,0)   
set @step2cmd = 'select count(*) from uat.ap.ztscm_protocollo' --+ @nometab
EXECUTE @rcount=sp_executesql @step2cmd
select @rcount
-2
 vMYQUERY := 'SELECT COUNT(*) FROM ALL_OBJECTS WHERE OWNER = UPPER(''MFI_IDBI2LIVE'') AND OBJECT_TYPE = ''TABLE'' 
    AND OBJECT_NAME  =''' || vTBL_CLIENT_MASTER || '''';
    PRINT_STRING(VMYQUERY);
    EXECUTE IMMEDIATE  vMYQUERY INTO VCOUNTTEMP ;
fancyPants
  • 50,732
  • 33
  • 89
  • 96
PaOne
  • 13
  • 1