Search code examples
sql-serverlinqpad

Script to generate SQL INSERT statements from SELECT statement in LinqPad


Is there any easy way to create individual SQL INSERT statements from each row of the results of a SELECT query in Linqpad, other than concatenation?

This is close to what I want but I don't think it accepts a SQL SELECT string as input.

This is also close to what I want but the output isn't individual INSERT statements.


Solution

  • You can use DumpAsInsert from your first suggestion.

    Because DumpAsInsert uses the IEnumerable more than once, you have to instantiate the query into memory:

    var sel = this.ExecuteQuery<Accounts>(@"Select * from Accounts Where Actid < 100").ToList();
    sel.DumpAsInsert("Accountscopy", "Actid");