What is the best way to do the equivalent of execute scalar in T-SQL? In other words, using SQL, how can I get the first column of the first row of the first result set when I call a stored procedure?
Edit:
Just to clarify, this is only using SQL, no client-side language. The result set could be something like:
Id Description
--------------
1 foo
2 bar
I only want “1” in a variable.
The only way I know how to do this is with a temp table (untested, but you get the idea):
INSERT #TempTable EXEC MyStoredProcedure
DECLARE @id INT;
SELECT TOP(1) @id = Id FROM #TempTable
if you’re talking about .net client, then you can use SQLCommand.ExecuteScalar.
if not please add an example of how this would look exactly for your case.
EDIT:
you can do this since you provided extra info:
However this is far from ideal, plus you have to enable ad hoc queries for this to work