Skip to content
Advertisement

Insert into table multiple rows with one different value

I believe my question is kinda simple but I haven’t found something like that upon searching. I want to insert into a table testing multiple rows for example:

(’03’,’ENG’,’203′) (’03’,’ENG’,’403′) (’03’,’ENG’,’155′) (’03’,’ENG’,’222′) (’03’,’ENG’,’456′) (’03’,’ENG’,’111′) …

Instead of writing

insert into testing
values('03','ENG','203')
,('03','ENG','403')
,('03','ENG','403')
,('03','ENG','155')
,('03','ENG','222')
,('03','ENG','456')
,('03','ENG','111')

..

Is there any faster way to write something like

insert  into testing
select ('03','ENG',x)
where x in ('203',...)

It would be even better if I did not have to use dynamic sql. Thanks in advance.

Advertisement

Answer

I’m not sure if it is that much simpler, but you can use insert . . . select with a values() clause for the third column:

insert into testing
    select '03', 'ENG', col3
    from (values ('203'), ('403'), ('155'), . . . 
         ) v(col3);

If you have a list of column3’s in a table and you have no duplicates, you can use:

insert into testing
    select '03', 'ENG', t.col3
    from column3_table t 
    where t.column3 in ('203', '403', '155', . . . );
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement