SQL Server Performance

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


Article Topics

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

USEFUL SITES :

ASP.NET Tutorials
Windows and SQL Azure Tutorials
Cloud Hosting Magazine
SharePoint Tutorials
Windows Server Help

Write for Us

Share your SQL Server knowledge with others and raise your profile in the community More...
Latest Articles

A High Level Comparison Between Oracle and SQL Server - Part ...
A High Level Comparison Between Oracle and SQL Server - Part ...
A High Level Comparison Between Oracle and SQL Server - Part ...
A High Level Comparison Between Oracle and SQL Server

More     
 
Latest FAQ's

Add Node to A SQL Server failover Cluster failed with invalid ...
SQL Server Destination remote server error
Setting Up Data And Log Files For SQL Server
Will Check Constraints Improve Database Performance?

More     
   
Latest Software Reviews

dbForge Review
Spotlight on ApexSQL Diff - Server-based database comparison tool ...
Spotlight on ApexSQL Data Diff - Server-based database comparison tool ...
Spotlight on ApexSQL Doc 2008

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

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.


        








C# Help and Tutorials | PHP MySQL Tutorial | Sharepoint Tutorial | Azure Tutorial | Cloud Hosting Magazine | ASP.NET Tutorials | Windows Server Help | Windows Phone Pro | Silverlight Ace | Visual Studio Tutorials | Home | Peformance Articles | Audit Articles | Business Intelligence Articles | Clustering Articles | Developer Articles | Reporting Services Articles | DBA Articles | ASP.NET / ADO.NET Articles | SQL Server Training Videos | 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 | 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


              © 2010 Jude O'Kelly. All rights reserved