Use a SELECT to Print a Bunch of INSERT INTOs

Posted by Mikecancook on Stack Overflow See other posts from Stack Overflow or by Mikecancook
Published on 2010-04-05T18:43:01Z Indexed on 2010/04/05 18:53 UTC
Read the original article Hit count: 201

Filed under:
|

I have a bunch of records I want to move to another database and I just want to create a bunch of inserts that I can copy and paste. I've seen someone do this before but I can't figure it out. I'm not getting the escapes right.

It's something like this where 'Code', 'Description' and 'Absent' are the columns I want from the table.

 SELECT 'INSERT INTO AttendanceCodes
          (Code, Description, Absent) 
         VALUES 
          (' + Code + ',' + Description  + ',' + Absent')'
  FROM AttendanceCodes 

The end result should be a slew of INSERTS with the correct values like this:

 INSERT INTO AttendanceCodes 
   (Code, Description, Absent) 
 VALUES 
   ('A','Unverified Absence','UA')

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server