SQL output of several local variables in one column

Is it possible, using SQL, to output several separate local variables in one column as separate rows? For instance.

DECLARE var1 INT = 4 DECLARE var2 INT = 5 DECLARE var3 INT = 6 

And then select the variables like this

 SELECT (var1, var2, var3) AS UserIDs, ('u1', 'u2', 'u3') AS Names 

This will create the following table:

 UserIDs | Names 4 | u1 5 | u2 6 | u3 
+6
source share
2 answers

Use table value constructor

 SELECT * FROM (VALUES (@var1,'u1'), (@var2,'u2'), (@var3,'u3')) tc (UserIDs, Names) 
+5
source
 select var1 as UserID, 'u1' as Name union all select var2, 'u2' union all etc etc 
+4
source

Source: https://habr.com/ru/post/1013296/


All Articles