0

I'm not sure how to ask this. I'm getting information in a stored procedure by running a select and saving the results into a variable, but is there a way to reduce this, all of this is inside a stored procedure.

SET @VAR1 = (SELECT PAR1 FROM TABLE1)
SET @VAR2 = (SELECT PAR2 FROM TABLE1)
SET @VAR3 = (SELECT PAR3 FROM TABLE1)

but could I get it instead some other way?

Like:

SET @VAR1, @VAR2, @VAR3 = (SELECT PAR1, PAR2, PART3 FROM TABLE1)
Dale K
  • 25,246
  • 15
  • 42
  • 71
  • 2
    [SELECT @local_variable (Transact-SQL)](https://learn.microsoft.com/en-us/sql/t-sql/language-elements/select-local-variable-transact-sql?view=sql-server-ver16) – Thom A Nov 16 '22 at 21:47

1 Answers1

2

Yes, you can do it like this:

SELECT
    @VAR1 = PAR1, 
    @VAR2 = PAR2,
    @VAR3 = PAR3
FROM 
    TABLE1

And you can see more in the official docs: SELECT @local_variable (Transact-SQL)

David Gaspar
  • 407
  • 5
  • 12
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459