Generate INSERT statements from a SQL Server Table

前端 未结 2 1488
情歌与酒
情歌与酒 2021-01-21 00:29

I have a table of 3.3 million records and don\'t want to copy the entire thing from dev to prod (on a client controlled machine and can\'t get the linked server working correctl

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-21 01:06

    I see you tagged your post SQL-Server-2005, that's too bad because version 2008 has a wizard tool for that.

    You could build the insert statements out of concatenated strings.

    If field1 is a string, field2 a numeric:

    select 'insert into data (field1, field2) values('' || field1 || '', ' || char(field2) ||');' from data where ID < 9000;
    

    Obviously that can be time-consuming if you have lots columns, considering that the strings needs quotes. You may have to convert the numeric columns using char() too.

    That should give you a list of insert statements, like this:

    insert into data (field1, field2) values('A', 10);
    insert into data (field1, field2) values('B', 20);
    insert into data (field1, field2) values('C', 30);
    

    Maybe that's not the most elegant way to do this, but it works.

提交回复
热议问题