Is there a way to set all variables with one set statment, like you can with a declare statement?
For example:
Declare @Test VARCHAR(10), @Test2 VARCHAR(10), @Test3 INT SET @Test = 'test' SET @Test2 = 'Test2' SET @Test3 = 1
Where I want to do something like the below, but the below does not work:
Set @Test = 'test', @Test2 = 'Test2', @Test3 = 3
You can assign the same value to multiple variables by using = consecutively. This is useful, for example, when initializing multiple variables to the same value. It is also possible to assign another value into one after assigning the same value.
Regarding feature of SQL Server where multiple variable can be declared in one statement, it is absolutely possible to do. From above example it is clear that multiple variables can be declared in one statement.
Variables in SQL procedures are defined by using the DECLARE statement. Values can be assigned to variables using the SET statement or the SELECT INTO statement or as a default value when the variable is declared. Literals, expressions, the result of a query, and special register values can be assigned to variables.
Instead of SET
, use SELECT
.
SELECT @Test = 'test', @Test2 = 'Test2', @Test3 = 3;
Here's a great article on SET vs SELECT in SQL Server / TSQL.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With