Site sponsored by: Idera Try Idera’s new SQL admin toolset
SQL Server Performance

  • Home
  • Articles
  • Forums
  • Tips
  • Quiz
  • FAQ's
  • Blogs
  • Software
  • Books
  • About Us
RSS Feeds
Sign in | Join


Article Topics

All Articles
Peformance Tuning
Audit
Business Intelligence
Clustering
Reporting Services
Developer
General DBA
ASP.NET / ADO.NET

SQL Server 2008 - Worth the Wait

SQL Server’s first significant upgrade in three years features a number of envelope-pushing enhancements and improvements. Which will have the greatest impact on SQL administration and development? More...
Latest Articles

Slowly Changing Dimensions in SQL Server 2005
Audit Data Modifications
SQL Server 2008’s Management Data Warehouse
Same Report but Different Methods in SQL Server Reporting Services ...

More     
 
Latest FAQ's

How to Integrate Performance Monitor and SQL Profiler
SSIS Lookups are Case Sensitive
Convert Number to Words in SSRS
After installing SP2 on SQL Server 2005 x64, when trying to ...

More     
   
Latest Software Reviews

SQL Server DBA Dashboard
SwisSQL DBChangeManager
SQLMesh - SQL Server Search Tool
SoftTreeTech SQL Assistant

More     

articles >> developer >> How to Write SQL to Dynamically Script ...

How to Write SQL to Dynamically Script Mass INSERT Statement Scripts

By : Josh Calderonello
Jul 26, 2002
Printer friendly

The following is an article that describes a unique approach of scripting out the contents of a table.

The reasons for doing so are many. The context that I will provide is one where you have a development environment where static tables exist for the purpose of storing application lookup data (i.e. a ZipCode table), but the contents of the table change often enough that it is often tedious to modify an INSERT script you may have saved off somewhere.

For instance, let’s suppose we have a ZipCode table that is defined in the following manner:

 

CREATE TABLE [dbo].[local_ZipCodes] 

(

[ZipCode_ID] [INT] NOT NULL,

[ZipCode] [VARCHAR] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,

[ZipPlus] [VARCHAR] (4) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,

) ON [PRIMARY]

GO

 

If the table defined above has several hundred entries to start out with during an initial development cycle, and several hundred more are added during each cycle, maintaining an INSERT script can get very tedious very quickly.

Let’s suppose our ZipCode table has the following values in it:

 

ZipCode_ID        ZipCode      ZipPlus

----------------- ------------ -----------

1                 43402        1111

2                 43403        2222

3                 43404        1111

. . .

. . .

650               89140        2358

 

One way in which a developer can quickly script out the contents of the ZipCode Table is in the following manner:

 

SELECT

'INSERT Local_ZipCodes (ZipCode_ID, ZipCode, ZipPlus) ' + 'SELECT ' +

CONVERT(VARCHAR(20), Loc_Zip.ZipCode_ID) + ', ' + '''' + CONVERT(VARCHAR(20), Loc_Zip.ZipCode) + '''' +

', ' + '''' + CONVERT(VARCHAR(4), Loc_Zip.ZipPlus) + ''''

FROM

dbo.Local_ZipCodes Loc_Zip

ORDER BY

Loc_Zip.ZipCode_ID ASC

 

When you run the above statement, your result set should look like the following:

 

INSERT Local_ZipCodes (ZipCode_ID, ZipCode, ZipPlus) SELECT 1, '43402', '1111'

INSERT Local_ZipCodes (ZipCode_ID, ZipCode, ZipPlus) SELECT 2, '43403', '2222'

INSERT Local_ZipCodes (ZipCode_ID, ZipCode, ZipPlus) SELECT 3, '43404', '1111'

INSERT Local_ZipCodes (ZipCode_ID, ZipCode, ZipPlus) SELECT 650, '89140', '2358'

 

That is all there is to it!

 

Published with the express written permission of the author. Copyright 2002.



Comments:
Your Name  
Email    
(Emails will not be displayed on the site or used for promotional purposes)
Comment  


Type characters in the image
 
 (case sensitive)

 
 
 

        








Home | Peformance Articles | Audit Articles | Business Intelligence Articles | Clustering Articles | Developer Articles | Reporting Services Articles | DBA Articles | ASP.NET / ADO.NET Articles | DBA FAQ's | Developer Peformance FAQ's | DBA Peformance FAQ's | Developer FAQ's | Clustering FAQ's | Error Messages | Audit Tool Reviews | Backup Tool Reviews | Coding Tool Reviews | Compare Tool Reviews | Documentation Tool Reviews | Design Tool Reviews | Monitoring Tool Reviews | Log Tool Reviews | Reporting Tool Reviews | Clustering Tool Reviews | Security Tool Reviews | Change Management Tool Reviews | Remote Access Tool Reviews | Book Reviews | Security Tool Reviews | QDPMA Performance Tuning | ADO.NET / ASP.NET | Administration | Analysis/OLAP Services | Application Development | Configuration | Components | ETL | Hardware | High Availability | Hints | Index | Misc | Operating Systems | Performance Tuning | Replication | T-SQL | Views