You can write arbitrary queries for your INSERT
, if you use SELECT
instead of VALUES
:
INSERT INTO MyTable
([Val1], [Val2], [Val3], [Val4])
SELECT 'val1','val2','val3',v4.v
FROM (SELECT 'val4_a' union all
SELECT 'val4_b' union all
SELECT 'val4_c' union all
SELECT 'val4_d') v4(v)
Of course, if you already have a table variable, say @v4
containing one row for each desired value, it's even easier:
INSERT INTO MyTable
([Val1], [Val2], [Val3], [Val4])
SELECT 'val1','val2','val3',v4.v
FROM @v4 v4
And of course, then, it can insert 1 row, 4 rows, 100000 rows, however many you've put in @v4
.
So, a complete script could be:
DECLARE @v4 table (v varchar(10) not null)
INSERT INTO @v4(v) VALUES ('val4_a'),('val4_b'),('val4_c'),('val4_d')
INSERT INTO MyTable
([Val1], [Val2], [Val3], [Val4])
SELECT 'val1','val2','val3',v4.v
FROM @v4 v4