Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute scalar in T-SQL

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
like image 342
Nelson Rothermel Avatar asked Jan 29 '26 13:01

Nelson Rothermel


1 Answers

Your temp table solution is probably the only option.

SQL isn't really designed for this

like image 179
gbn Avatar answered Feb 02 '26 15:02

gbn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!