Link to home
Start Free TrialLog in
Avatar of FrancineTaylor
FrancineTaylor

asked on

Can I get an int value returned from EXEC?


DECLARE @count int
DECLARE @cmd nvarchar(500)
SET @cmd = 'SELECT COUNT(*) FROM MyTable WHERE ID  < 0'

Given these variables, how can I get the integer number returned by the SQL in @cmd into @count?
Avatar of Eyal
Eyal
Flag of Israel image

SELECT @count = COUNT(*) FROM MyTable WHERE ID  < 0
Avatar of FrancineTaylor
FrancineTaylor

ASKER

I need to use the text in the @cmd variable to get the value into @count.  I don't know until I get to this section what is going to be contained in @cmd, only that in int value will be returned
insert the result to temporary table and query the temporary table
I was hoping to do it without having to create a temporary table.  Never mind, I found my answer:

DECLARE @count int
DECLARE @cmd nvarchar(500)
SET @cmd = 'SELECT COUNT(*) FROM MyTable WHERE ID  < 0'

DECLARE @ParamDef nvarchar(1000), @rtnVal nvarchar(1000)
Select @ParamDef = '@rtnVal nvarchar(1000) OUTPUT'
EXEC dbo.sp_executesql @cmd, @ParamDef, @rtnVal OUTPUT
SET @count = CAST(@rtnVal as int)


However, I'd be happy to accept your answer as a solution if you will include code for it that starts out with my three statements and ends up with @count containing the answer.
Oops, I didn't copy my set @cmd statement quite correctly...the correct code is:

DECLARE @count int
DECLARE @cmd nvarchar(500)
SET @cmd = 'SELECT @rtnVal = COUNT(*) FROM MyTable WHERE ID  < 0'

DECLARE @ParamDef nvarchar(1000), @rtnVal nvarchar(1000)
Select @ParamDef = '@rtnVal nvarchar(1000) OUTPUT'

EXEC dbo.sp_executesql @cmd, @ParamDef, @rtnVal OUTPUT
SET @count = CAST(@rtnVal as int)
Avatar of Anthony Perkins
Is there any reason you insist on resorting to Dynamic SQL.  You do realize that will mean the user will need SELECT permissions on MyTable, right?  In general, large shops do not typically permit the use of Dynamic SQL due to the inherent risk with SQL Injection and because of the lousy performance it entails.
As you don't know the value contained in the @cmd this has a huge chance it won't work
as the value must contain your output variable and it should fill it

I would suggest doing it more Dynamically so you build the Query yourself inside your Procedure.
This has the benefit that :
 * You make the Query so you can make sure it works (Performant)
 * You are sure that it fills your output variable

ASKER CERTIFIED SOLUTION
Avatar of MrNetic
MrNetic
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Francine answer is good also, my answer does what you want without changing to much code.
Just what I wanted...thanks!
I have to report that I was mistaken about what the solution was.  Mr Netic's solution seemed to work, but I wasn't paying attention.  When the code is executed, the correct number is output, but it doesn't actually get assigned to @count.  So you need to use my corrected code:

DECLARE @count int
DECLARE @cmd nvarchar(500)
SET @cmd = 'SELECT @rtnVal = COUNT(*) FROM MyTable WHERE ID  < 0'

DECLARE @ParamDef nvarchar(1000), @rtnVal nvarchar(1000)
Select @ParamDef = '@rtnVal nvarchar(1000) OUTPUT'

EXEC dbo.sp_executesql @cmd, @ParamDef, @rtnVal OUTPUT
SET @count = CAST(@rtnVal as int)