views:

108

answers:

3

It seems like this would would be trivial, but my SQL is not up to par and I can't figure it out. I just want to select a returned scalar from a stored procedure. Basically like this:

select dbo.sProcedure @param

But that throws an error. It's not really an option to return a result set from within the procedure. Is it possible to do this, or do I need to start rewriting some code?

+1  A: 

You should pass the sp an output parameter where you can store the required returned value, or use an scalar function

j.a.estevan
+6  A: 

Something like this:

DECLARE @SomeReturnValue INT
EXEC @SomeReturnValue = SomeProc
SELECT @SomeReturnValue
RichardOD
Note that while this works as a SQL query, if you're in a programming environment such as C#, there are better ways to get the return value.
Thanatos
Yeap. There are no C# tags on the question though!
RichardOD
That's interesting -- I actually am using C# for this, and this solution is working fine for me (creating a DbCommand with this text and calling executeScalar). But what's the better way?
Ian Henry
@Ian: You would create a parameter and set the direction to `ParameterDirection.ReturnValue`. http://msdn.microsoft.com/en-us/library/system.data.parameterdirection.aspx
LukeH
A: 

@RichardOD has the right syntax, however as implied, you can't use it in a select statement. If you need to use it in a select statement, then use a function instead of a stored procedure.

Yishai