When I need to store the result of a SQL sentence in a variable I can use a code like so
declare @Max as int
set @Max=(select max(field_name) from table_name)
But now i wondering if is possible do the same for multiple variables for example for a sentence like
select max(field_name), min(field_name) from table_name
how i can store the result in two variables called @Max and @Min?
I know which this can be done using cursors
DECLARE @Max as int, @Min as int
DECLARE cursor_minmax CURSOR FOR
Select max(field_name), min(field_name) from table_name
OPEN cursor_minmax
FETCH NEXT FROM cursor_minmax INTO @Max,@Min
CLOSE cursor_minmax
DEALLOCATE cursor_minmax
but i want to know if is possible assign that values wihout use cursors.
You can do this as:
I see . . . your confusion is that you want to use “set”. You can only set one value with “set”. You can set as many values as you like with “select”.