Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Insert Generator - Stored Procedure

By Nick Greene,

SQL server management studio interface provides the ability to create insert scripts for tables, but I needed a way to automate this insert script building process. I ended up writing my own stored procedure to allow me to do this. It accepts 5 parameters:

  1. @schemaName - The schema the table belongs to
  2. @tableName - The name of the table
  3. @IncludePrimaryKeyIdentity - True/False flag to include a primary key that is an identity value.  Defaults to False because typically you would want this to self generate.
  4. @IncludeNonPrimaryKeyIdentity - True/False flag to include a primary key that is not an identity value.  Defaults to True because if not auto generating you would want to include this.
  5. @IncludeIfNotExists - True/False flag for whether to include IF NOT EXISTS() statement around each INSERT statement.

I have not built this to support ALL data types, but it will support, what I consider to be, the most common data types.    If you attempt to execute it on a table that contains an unsupported datatype it will error out and inform you it does not support the datatype.  I use it primarily in SQL 2008, but it should work in 2005.  I have not tested it in 2012, but I would expect it to work.

I have also posted this on my blog at

Total article views: 1955 | Views in the last 30 days: 7
Related Articles

Security Awareness for Developers - What would you include?

If you were putting together a security awareness training program for developers, what aspects of ...


Foreign Language Support.

Foreign Language Support.


I would like to insert from the clipboard text with line breaks in a "varchar" column.

I would like to insert from the clipboard text with line breaks in a "varchar" column.


Insert rows



production Support

Help needed in production support


Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones