Multiple Dynamic Inserts with SQL

August 30, 2005
databases

If you have ever had to do lots of SQL INSERT statements you will know that they can get pretty slow. When looking for ways to speed up some inserts, I noticed that you can use a SELECT subquery to provide the values. Like this:

INSERT INTO foo_archive (id, title)
  SELECT id, title
  FROM foo

This should work on Microsoft SQL Server, MySQL, PostgreSQL, and Oracle.



Related Entries

7 people found this page useful, what do you think?

Comments

I've done these types of inserts on IBM's DB2 also. It's a great trick when normalizing poorly designed tables, or to force a reorder when adding indices to a table.
I WANT TO INSERT 100 RECORDS AT A TIME ..HOW CAN I BUILD THIS QUERY??

Post a Comment




  



Spell Checker by Foundeo

Recent Entries



foundeo


did you hack my cf?