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
Developer
General DBA
ASP.NET / ADO.NET

Write for Us

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

Working with Windows Communication Foundation (WCF)
Transfer Logins Task and Transfer Database Task in SSIS
Practical Database Change Management (Part 2)
Practical Database Change Management (Part 1)

More     
 
Latest FAQ's

ALTER TABLE SWITCH statement failed because column '%.*ls' has data type ...
ALTER TABLE SWITCH statement failed because column '%.*ls' has data type ...
ALTER TABLE SWITCH statement failed. There is no identical index in ...
'%ls' statement failed because the expression identifying partition number for the ...

More     
   
Latest Software Reviews

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

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.


        








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 | 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


              © 1999-2008 by T10 Media. All rights reserved