October 21, 2014 at 7:54 am
Assume I built a stored proc (dbo.testproc) that will return the OUTPUT parameter @RandomInteger.
I could pass a specific value for the parameter...
EXEC dbo.testproc 7
Or I could return a value from the proc...
DECLARE @ReturnInteger
EXEC dbo.testproc @RandomInteger = @ReturnInteger OUTPUT
SELECT @ReturnInteger
But I want to do both which, if this actually worked, might look like this...
DECLARE @RandomInteger
SET @RandomInteger = 7
EXEC dbo.testproc @RandomInteger = @ReturnInteger OUTPUT
SELECT @ReturnInteger
I want to pass a specific value and return the result from the proc. Do I need to use two parameters for this?
Thanks.
October 21, 2014 at 8:10 am
If it's of any interest, here is the vague scenario that fits my original question.
Normally, this proc has two possibilities. 1) The parameter is known so we pass it to the proc. We don't need to return the value because we already know it. 2) The parameter is not known so it is not passed to the proc. We need to return the value because we don't know it.
However, a third case has come up. We may know the value of the parameter (i.e. option 1 above) but the proc may identify an issue with the data which requires the input proc to change the parameter value in which case we want that value returned (i.e. option 2 above).
October 21, 2014 at 8:11 am
Rather use two parameters. It's one of those good coding practices, a parameter should be one thing only.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
October 21, 2014 at 8:28 am
Fair enough, Gila Monster.
FWIW, I found this...
http://technet.microsoft.com/en-us/library/ms187004(v=sql.105).aspx
"Input values can also be specified for OUTPUT parameters when the stored procedure is executed. This allows the stored procedure to receive a value from the calling program, change it or perform operations with it, then return the new value to the calling program."
As I read that section, it appears that I should be able to do what I want to do but I don't understand how.
October 21, 2014 at 8:40 am
Hooray! I figured it out.
The thing is to set the OUTPUT parameter as noted below, which I was doing backwards.
DECLARE @ReturnInteger
SET @ReturnInteger = 7
EXEC dbo.testproc @RandomInteger = @ReturnInteger OUTPUT
SELECT @ReturnInteger
October 21, 2014 at 8:42 am
I'm with Gail. Two parameters. At some point you may refactor this and you'll want the separation.
October 21, 2014 at 10:01 am
Wouldn't it be easier and more natural to refactor this as a function?
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
October 21, 2014 at 10:10 pm
ChrisM@Work (10/21/2014)
Wouldn't it be easier and more natural to refactor this as a function?
I agree but to return a random number, you'd probably want to use a GUID as the random seed. I'm pretty sure than NEWID() still isn't allowed in a function in 2012 but you could always use the "VIEW" trick to get a GUID.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply