I want to declare a table variable in sql server which will have some hard coded values in it. I tried:
DECLARE @tbl TABLE(Id int) = (1, 2, 3, 4, 5) -- gives error
and this didn't work either:
DECLARE @tbl TABLE(Id int)
INSERT INTO @TBL SELECT * FROM (1, 2, 3, 4, 5) -- also gives error
isn't there any way to create a table with hard coded existing values?
The syntax you want is:
DECLARE @tbl TABLE(Id int);
INSERT INTO @tbl (id)
VALUES (1), (2), (3), (4), (5);
Here is a db<>fiddle.
In case your data is a string
DECLARE @tbl TABLE(Id int)
INSERT INTO @TBL
SELECT value from string_split('1, 2, 3, 4, 5',',')
Select * from @tbl
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