Click here to Skip to main content
Click here to Skip to main content

Generating INSERT statements in SQL Server

By , 16 Jan 2005
 

Introduction

The stored procedure InsertGenerator generates the INSERT..VALUES statements for the specified table name.

Background

SQL Server doesn’t allow generation of INSERT statements for the table data, when Generate SQL Script option is selected. The workaround is to make use of DTS for transferring data across servers. However, there exists a need to generate INSERT statements from the tables for porting data. Simplest example is when small or large amount of data needs to be taken out on a removable storage media and copied to a remote location, INSERT..VALUES statements come handy.

Using the code

This small yet useful stored procedure will take as parameter the table name and generates the INSERT SQL statements for the same. The output can be redirected to either text format (Ctrl+T in Query Analyzer) or Output to a text file. The procedure accepts an input varchar type parameter that has to be the table name under consideration for statement generation.

CREATE PROC InsertGenerator
(@tableName varchar(100)) as

Then it includes a cursor to fetch column specific information (column name and the data type thereof) from information_schema.columns pseudo entity and loop through for building the INSERT and VALUES clauses of an INSERT DML statement.

--Declare a cursor to retrieve column specific information 
--for the specified table
DECLARE cursCol CURSOR FAST_FORWARD FOR 
SELECT column_name,data_type FROM information_schema.columns 
    WHERE table_name = @tableName
OPEN cursCol
DECLARE @string nvarchar(3000) --for storing the first half 
                               --of INSERT statement
DECLARE @stringData nvarchar(3000) --for storing the data 
                                   --(VALUES) related statement
DECLARE @dataType nvarchar(1000) --data types returned 
                                 --for respective columns
SET @string='INSERT '+@tableName+'('
SET @stringData=''

DECLARE @colName nvarchar(50)

FETCH NEXT FROM cursCol INTO @colName,@dataType

IF @@fetch_status<>0
    begin
    print 'Table '+@tableName+' not found, processing skipped.'
    close curscol
    deallocate curscol
    return
END

WHILE @@FETCH_STATUS=0
BEGIN
IF @dataType in ('varchar','char','nchar','nvarchar')
BEGIN
    SET @stringData=@stringData+'''''''''+
            isnull('+@colName+','''')+'''''',''+'
END
ELSE
if @dataType in ('text','ntext') --if the datatype 
                                 --is text or something else 
BEGIN
    SET @stringData=@stringData+'''''''''+
          isnull(cast('+@colName+' as varchar(2000)),'''')+'''''',''+'
END
ELSE
IF @dataType = 'money' --because money doesn't get converted 
                       --from varchar implicitly
BEGIN
    SET @stringData=@stringData+'''convert(money,''''''+
        isnull(cast('+@colName+' as varchar(200)),''0.0000'')+''''''),''+'
END
ELSE 
IF @dataType='datetime'
BEGIN
    SET @stringData=@stringData+'''convert(datetime,''''''+
        isnull(cast('+@colName+' as varchar(200)),''0'')+''''''),''+'
END
ELSE 
IF @dataType='image' 
BEGIN
    SET @stringData=@stringData+'''''''''+
       isnull(cast(convert(varbinary,'+@colName+') 
       as varchar(6)),''0'')+'''''',''+'
END
ELSE --presuming the data type is int,bit,numeric,decimal 
BEGIN
    SET @stringData=@stringData+'''''''''+
          isnull(cast('+@colName+' as varchar(200)),''0'')+'''''',''+'
END

SET @string=@string+@colName+','

FETCH NEXT FROM cursCol INTO @colName,@dataType
END

After both of the clauses are built, the VALUES clause contains a trailing comma which needs to be replaced with a single quote. The prefixed clause will only face removal of the trailing comma.

DECLARE @Query nvarchar(4000) -- provide for the whole query, 
                              -- you may increase the size

SET @query ='SELECT '''+substring(@string,0,len(@string)) + ') 
    VALUES(''+ ' + substring(@stringData,0,len(@stringData)-2)+'''+'')'' 
    FROM '+@tableName
exec sp_executesql @query --load and run the built query

Eventually, close and de-allocate the cursor created for columns information.

CLOSE cursCol
DEALLOCATE cursCol

After the procedure is compiled and created, just run it in Query Analyzer by using the following syntax:

InsertGenerator <tablename>

E.g.:

USE pubs
GO
InsertGenerator employee 
GO

Then copy the INSERT statements and run’em in the query analyzer.

Before the INSERTs are run, SET IDENTITY_INSERT <TABLENAME>ON should be passed for adding values in an identity-based column.

Points of Interest

D: T-SQL lovers might wish to extend this procedure for providing support for binary data types such as IMAGE etc.

History

  • Ver 0.1b added Dec 5, 03.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sumit Amar .
Software Developer (Senior)
United States United States
Member
Sumit Amar started programming in BASIC at the age of 14 in 1993, then moved on to C/UNIX.
Later in 1999, he started developing commercial applications in J2SE, J2EE and Perl. He started developing applications in .NET with ASP+ (later renamed to ASP.NET) in December 2000 with public Beta 1. He's continuing development in .NET (ASP.NET, C#) ever since.
 
He has an MBA degree in IT and Systems besides his 200+ certifications including MCSD.NET, SCJP, MCTS, MCITP (Administrator and Developer), SCWCD, OCP-DBA and numerous Brainbench certifications (Transcript ID 2232802, World rank 3).
 
He is a Research Software Development Engineer at Microsoft Corporation, Redmond WA, where he works majorly on AJAX, .NET, SOAP, Web Services and Secure applications.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionGenerate insert for varbinary fieldsprofessionalDiana NoWay16 Apr '13 - 9:28 
Your script is really useful, thanks!
But I'm trying to solve this problem for about 2 days now (I'm a beginner at SQL Server):
One of my tables have a varbinary column used to store an image and I can't find a way to get the hex from the field in this insert generator...
Please, can you help me?
Lead Singer of No Way band www.brnoway.com

QuestionThanksmemberEddy Nijs15 Oct '12 - 20:42 
The procedure works fine. Thanks for sharing.
GeneralthnksmemberPayitax123316 Aug '12 - 15:03 
Thanks for the effort! works for me!
GeneralMy vote of 5memberPayitax123316 Aug '12 - 15:01 
works!
QuestionAwesome!memberjaaaaaac3 Jul '12 - 4:50 
This worked perfectly for me, thanks a million
Questioni would say AWESOME dudememberNikhil Bhivgade25 Jun '12 - 1:13 
i would say AWESOME dude
 
thanks man great job.
NewsSQL Server Management Studio does this.memberAlexandru Lungu2 May '12 - 1:50 
Quote:
SQL Server doesn’t allow generation of INSERT statements for the table data, when Generate SQL Script option is selected.

This is an inaccurate statement... nowadays! I assume that almost 10 years ago when the article was first published, that was the case, but not anymore.
 
SQL Server Management Studio (even the express edition) does this. But not by default.
 
You're looking for this Types of data to script from the Generate and Publish Scripts wizard:
 
(Right click on db -> Tasks -> Generate Scripts -> Set Scripting Options -> Advanced -> Types of data to script: (Schema only (default) | Data only | Schema and data))
 
.

QuestionNULLs are not handled correctly. Causing issues with nullabel UniqueIdentifier columns.memberRS Reddy220 Apr '12 - 12:30 
Good work by the author.
 
This solution should work most scenarios however its not handling NULLs right.

For others I would suggest, try this.

SQL Statement Generator[^]
 
[^]
Here also you need to fix the c# code to handle Guid.
AnswerRe: NULLs are not handled correctly. Causing issues with nullabel UniqueIdentifier columns.memberbencejoful12 Jun '12 - 8:17 
Here is a fix for the NULL problem you mentioned. This also fixes a bug that happens when the data contains a single-quote (').
 
IF @dataType in ('varchar','char','nchar','nvarchar')
BEGIN
    SET @stringData=@stringData+'
        isnull(''''''''+REPLACE('+@colName+','''''''','''''''''''')+'''''''',''NULL'')+'',''+'
END
ELSE
if @dataType in ('text','ntext') --if the datatype
                                 --is text or something else
BEGIN
    SET @stringData=@stringData+'
          isnull(''''''''+REPLACE(cast('+@colName+' as varchar(2000)),'''''''','''''''''''')+'''''''',''NULL'')+'',''+'
END

SuggestionReady to use Stored Procedure for generating INSERT statementsmemberMohammed_Rashid12 Mar '12 - 21:04 
This post [^]may be helpful. You can generate the INSERT statements for a table with applying filter and even the order in which you want the output data. It's a ready to use parameterized Stored Procedure. It does not use cursors and hence faster. It works fine with all data types.
Questionsimply awesome manmemberFarhan Asif12 Mar '12 - 4:09 
I was having a DB of around 120 MB and with lot of transaction, it took me two days to normalize the data.Again while uploading the data to new database i was into numerous problems. Thanks to your awesome script, it took only two and half hours for me to upload the data.....
this is Normal

QuestionAnother tool with more featuresmemberRishikesh_716 Feb '12 - 3:42 
This tool allows to create Insert script on multiple tables at one go .. with multiple filter conditons.
Creates IF NOT EXIST statement before inserting data
Generate Insert script to extract data[^]
QuestionGenerate Insert script using C# : Check this outmemberRishikesh_716 Feb '12 - 3:39 
Generate Insert script to extract data[^]
GeneralMy vote of 5 [modified]memberAmol_B7 Feb '12 - 1:00 
Thanks a lot.

modified 7 Feb '12 - 7:29.

GeneralMy vote of 5memberbtrain24 Jan '12 - 12:51 
This was exactly what I was looking for and worked just as I hoped.
QuestionHow to add a WHERE parammemberMember 63398310 Jan '12 - 2:47 
If the table has an INT primary key how is the WHERE clause coded so only values > n are selected? I can't seem to get this right.
AnswerRe: How to add a WHERE parammemberHawkeye367718 Apr '12 - 12:00 
I was able to add a where clause this way:
ALTER PROC [dbo].[InsertGenerator]
@tableName varchar(100),
@whereClause VARCHAR(100)
as
then modified the SET @query line this way:
SET @query ='SELECT '''+substring(@string,0,len(@string)) + ') VALUES(''+ ' +
substring(@stringData,0,len(@stringData)-2)+'''+'')'' FROM '+@tableName +
 ' WHERE ' + @whereClause
exec sp_executesql @query
 
To execute the proc:
 
USE testbed
go
EXEC insertgenerator accident,'ID IN (10,17,19)'
GO
Steve Hochreiter

GeneralExcellent! Thanks for sharingmemberNatreen12 Dec '11 - 2:57 
Well done!! Very efficient coding!!
GeneralThanks!memberMember 84519141 Dec '11 - 15:29 
This is awesome! Saved a lot of my time Smile | :) Good job!!! Thumbs Up | :thumbsup:
My vote of 5 too....
QuestionThanks lotmemberbasheer97127 Oct '11 - 0:44 
Very nice article, its really helped me and saved my time
QuestionThank you, Thank You :)memberBasil Ntinga20 Oct '11 - 20:33 
just what i needed, i had the colname edited to have [] in case the table design had reserved words. many thanks
QuestionHandling XML DatatypememberBadcop66619 Jun '11 - 12:45 
Hi, apologies to anyone else who has solved this:-
 
added in:-
 
IF @dataType='xml'
BEGIN
	SET @stringData= @stringData+'''''''''+isnull(cast(convert(xml,'+@colName+') as varchar(max)), NULL)+'''''',''+' 
END
 
works well. Thanks! HUGE timesaving over using the ssms 2008 script wizard.
 
Andy
AnswerRe: Handling XML DatatypememberBadcop66619 Jun '11 - 15:42 
Hi again, this has been a wrestle, the quoting could send you over the edge on a bad day Laugh | :laugh:
 
here's the whole thing - which copes properly with very large xml data blocks:-
 
USE [******]
GO
/****** Object:  StoredProcedure [dbo].[InsertGenerator]    Script Date: 06/20/2011 10:23:26 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
 
IF EXISTS (SELECT * FROM sys.objects WHERE type = 'P' AND name = 'InsertGenerator')
DROP PROCEDURE InsertGenerator
GO
 
CREATE PROC [dbo].[InsertGenerator]
(@tableName varchar(100)) as
 
--Declare a cursor to retrieve column specific information for the specified table
DECLARE cursCol CURSOR FAST_FORWARD FOR 
SELECT column_name,data_type FROM information_schema.columns WHERE table_name = @tableName
OPEN cursCol
DECLARE @string nvarchar(max) --for storing the first half of INSERT statement
DECLARE @stringData nvarchar(max) --for storing the data (VALUES) related statement
DECLARE @dataType nvarchar(max) --data types returned for respective columns
DECLARE @snippet nvarchar(max)
SET @string='INSERT '+@tableName+'('
SET @stringData=''
 
DECLARE @colName nvarchar(50)
 
FETCH NEXT FROM cursCol INTO @colName,@dataType
 
IF @@fetch_status<>0
	begin
	print 'Table '+@tableName+' not found, processing skipped.'
	close curscol
	deallocate curscol
	return
END
 
WHILE @@FETCH_STATUS=0
BEGIN
 
	--PRINT 'Column ' + @colname + ' is ' + @dataType

IF @dataType in ('varchar','char','nchar','nvarchar')
BEGIN
	--SET @stringData=@stringData+'''''''''+isnull('+@colName+','''')+'''''',''+'
	SET @stringData=@stringData+''''+'''+isnull('''''+'''''+'+@colName+'+'''''+''''',''NULL'')+'',''+'
END
ELSE
if @dataType in ('text','ntext') --if the datatype is text or something else 
BEGIN
	SET @stringData=@stringData+'''''''''+isnull(cast('+@colName+' as varchar(2000)),'''')+'''''',''+'
END
ELSE
IF @dataType = 'money' --because money doesn't get converted from varchar implicitly
BEGIN
	SET @stringData=@stringData+'''convert(money,''''''+isnull(cast('+@colName+' as varchar(200)),''0.0000'')+''''''),''+'
END
ELSE 
IF @dataType='datetime'
BEGIN
	--SET @stringData=@stringData+'''convert(datetime,''''''+isnull(cast('+@colName+' as varchar(200)),''0'')+''''''),''+'
	--SELECT 'INSERT Authorizations(StatusDate) VALUES('+'convert(datetime,'+isnull(''''+convert(varchar(200),StatusDate,121)+'''','NULL')+',121),)' FROM Authorizations
	--SET @stringData=@stringData+'''convert(money,''''''+isnull(cast('+@colName+' as varchar(200)),''0.0000'')+''''''),''+'
	SET @snippet='''convert(datetime,'+'''+isnull('''''+'''''+convert(varchar(200),'+@colName+',121)+'''''+''''',''NULL'')+'',121),''+'
  --                             'convert(datetime,'+isnull(''''+convert(varchar(200),StatusDate,121)+'''','NULL')+',121),)' FROM Authorizations
	--PRINT @snippet
	SET @stringData = @stringData+@snippet
END
ELSE 
IF @dataType='image' 
BEGIN
	SET @stringData=@stringData+'''''''''+isnull(cast(convert(varbinary,'+@colName+') as varchar(6)),''0'')+'''''',''+'
END
ELSE
IF @dataType='xml'
BEGIN
	SET @snippet='''''''''+REPLACE(cast('+@colName+' as varchar(max)),'''''''','''''''''''')+'''''''',+'
	--PRINT @snippet
	SET @stringData = @stringData+@snippet
END
ELSE --presuming the data type is int,bit,numeric,decimal 
BEGIN
	--SET @stringData=@stringData+'''''''''+isnull(cast('+@colName+' as varchar(200)),''0'')+'''''',''+'
	--SET @stringData=@stringData+'''convert(datetime,'+'''+isnull('''''+'''''+convert(varchar(200),'+@colName+',121)+'''''+''''',''NULL'')+'',121),''+'
	SET @stringData=@stringData+''''+'''+isnull('''''+'''''+convert(varchar(200),'+@colName+')+'''''+''''',''NULL'')+'',''+'
END
 
SET @string=@string+@colName+','
 
--PRINT @string

FETCH NEXT FROM cursCol INTO @colName,@dataType
END
DECLARE @Query nvarchar(max)
 
SET @query ='SELECT '''+substring(@string,0,len(@string)) + ') VALUES(''+ ' + substring(@stringData,0,len(@stringData)-2)+'''+'')'' FROM '+@tableName
--print @query
exec sp_executesql @query
--select @query

CLOSE cursCol
DEALLOCATE cursCol
 

GeneralRe: Handling XML DatatypememberBadcop66619 Jun '11 - 15:45 
one more point, I think this could be made really robust with a little more work on columns sizes - avoiding truncation - some arbitrary sizes have been used here.
 
thx
 
Andy
GeneralMy vote of 5memberPolash.IT6 Jun '11 - 11:42 
Nice work.
Thanks
Polash

GeneralMy vote of 5memberGypo2 Jun '11 - 4:50 
Excellent, saved me a lot of work, thanks
GeneralMy vote of 5memberjaguarOax17 May '11 - 10:32 
Muy buena ayuda para la transportacion de datos entre manejadores
GeneralMy vote of 5memberJP®16 May '11 - 3:43 
Nice Code.
GeneralMy vote of 5memberGeorge Kousis8 May '11 - 1:30 
practical and to the point. thanks
GeneralDoes not work with local or global temporary tables (ie #foo ##foo)memberJafin30 Mar '11 - 17:47 
Great script!
 
It appears not to work with temp tables.
GeneralINTO Qualifier MissingmemberRogerDodge18 Feb '11 - 6:41 
Excellent and very useful script but you need to add an "INTO" qualifier in Line
 
SET @string='INSERT INTO '+@tableName+'('
 
Smile | :)
GeneralRe: INTO Qualifier MissingmemberSumit Amar .25 May '11 - 10:53 
I only tested in SQL Server and INTO clause is not required there.
GeneralMy vote of 5memberr sukhadiya1 Feb '11 - 21:06 
Hey dude great solution thanks a lot keep making this type of articles thanks a lot one again..
GeneralMy vote of 5memberdidilcool28 Jan '11 - 5:29 
Goooood
GeneralMy vote of 5memberYatin Barmeda15 Jan '11 - 3:58 
Very useful. Thanks Sumit.
GeneralMy vote of 5memberpavan1233219 Dec '10 - 1:53 
It Rocked
GeneralGreat! Thank you!membervnts722 Nov '10 - 23:17 
Great! Thank you!
GeneralMy vote of 5memberMember 164164223 Sep '10 - 4:28 
Very good article
GeneralMy vote of 5memberbytejanitor10 Aug '10 - 4:30 
Great tool
QuestionReally long stringsmemberbytejanitor9 Aug '10 - 13:34 
Thanks Sumit.
This works great but my tables are so long it has issues.
I went to vchar and it was still not enough.
I'm having trouble trying to do text with a local variable issue.
Any thoughts.
Thanks Laugh | :laugh:
AnswerRe: Really long stringsmemberMember 85414645 Jan '12 - 7:06 
Same problem here.
GeneralThank you for saving me time!memberAdrian Akison8 Jun '10 - 16:42 
Almost exactly what I was looking for and saved me a heap of time.
 
I added a second parameter to pass an optional where clause through to constrain the output for my purposes as well, but was easy to do.
 
Cheers, Adrian
GeneralTks a Lot!memberFernandoMartin18 Nov '09 - 2:10 
Tks a Lot for this useful script! Big Grin | :-D Big Grin | :-D
GeneralThanks !!!memberaqnysos12329 Sep '09 - 5:11 
Thanks for the script.
GeneralThanks a lot!memberUday Takbhate7 Aug '09 - 2:41 
I had a unique situation where I had to migrate a SQL DB to a server where I did not have any access but a web SQL prompt.
The only way to migrate data was to execute the SQL insert script on the web SQL prompt.
 
Your script helped a lot.
 
-Uday
 
udyaa

GeneralThank you!memberBruceL21 May '09 - 5:24 
Thanks very much for this slick and very efficient piece of code. It saved me hours of work and I really appreciate your talent and creativity. Great code!
Again, Thanks!!!
QuestionHow to emulate SQLPUBWIZ? [modified]memberapergiel11 May '09 - 12:10 
How can I get this to emulate SQLPUBWIZ?
I have five tables that I would like to script a INSERT query for.
The resulting file would contain the INSERT code for all five tables.
Perhaps a batch file that comes loaded with the 5 file names?
Which in turn generates a 2nd .sql file.
Which would then be executed on the remote Host.
My attempts to do this were frustrated by the PRINT command getting confused with special characters ( ',", etc), When I was attempting to have TSQL code written to the resulting script file.
Thanks
 
modified on Tuesday, May 12, 2009 7:23 AM

Generalfine Script, updated with schema of tablememberspider_pat5 Mar '09 - 1:26 
with the little changes of table_schema for the table, so i can use this script now.
 
------------------------------------------------------------------------------------

CREATE PROC InsertGenerator
(@tableName varchar(100)) as
 
--Declare a cursor to retrieve column specific information for the specified table
DECLARE cursCol CURSOR FAST_FORWARD FOR
SELECT column_name,data_type,table_schema FROM information_schema.columns WHERE table_name = @tableName
OPEN cursCol
DECLARE @string nvarchar(3000) --for storing the first half of INSERT statement
DECLARE @stringData nvarchar(3000) --for storing the data (VALUES) related statement
DECLARE @dataType nvarchar(1000) --data types returned for respective columns
DECLARE @schemaType nvarchar(100) --table schema value for correct schema execution
SET @stringData=''
 
DECLARE @colName nvarchar(50)
 
FETCH NEXT FROM cursCol INTO @colName,@dataType,@schemaType
 
IF @@fetch_status<>0
begin
print 'Table '+@tableName+' not found, processing skipped.'
close curscol
deallocate curscol
return
END
 
WHILE @@FETCH_STATUS=0
BEGIN
IF @dataType in ('varchar','char','nchar','nvarchar')
BEGIN
--SET @stringData=@stringData+'''''''''+isnull('+@colName+','''')+'''''',''+'
SET @stringData=@stringData+''''+'''+isnull('''''+'''''+'+@colName+'+'''''+''''',''NULL'')+'',''+'
END
ELSE
if @dataType in ('text','ntext') --if the datatype is text or something else
BEGIN
SET @stringData=@stringData+'''''''''+isnull(cast('+@colName+' as varchar(2000)),'''')+'''''',''+'
END
ELSE
IF @dataType = 'money' --because money doesn't get converted from varchar implicitly
BEGIN
SET @stringData=@stringData+'''convert(money,''''''+isnull(cast('+@colName+' as varchar(200)),''0.0000'')+''''''),''+'
END
ELSE
IF @dataType='datetime'
BEGIN
--SET @stringData=@stringData+'''convert(datetime,''''''+isnull(cast('+@colName+' as varchar(200)),''0'')+''''''),''+'
--SELECT 'INSERT Authorizations(StatusDate) VALUES('+'convert(datetime,'+isnull(''''+convert(varchar(200),StatusDate,121)+'''','NULL')+',121),)' FROM Authorizations
--SET @stringData=@stringData+'''convert(money,''''''+isnull(cast('+@colName+' as varchar(200)),''0.0000'')+''''''),''+'
SET @stringData=@stringData+'''convert(datetime,'+'''+isnull('''''+'''''+convert(varchar(200),'+@colName+',121)+'''''+''''',''NULL'')+'',121),''+'
-- 'convert(datetime,'+isnull(''''+convert(varchar(200),StatusDate,121)+'''','NULL')+',121),)' FROM Authorizations
END
ELSE
IF @dataType='image'
BEGIN
SET @stringData=@stringData+'''''''''+isnull(cast(convert(varbinary,'+@colName+') as varchar(6)),''0'')+'''''',''+'
END
ELSE --presuming the data type is int,bit,numeric,decimal
BEGIN
--SET @stringData=@stringData+'''''''''+isnull(cast('+@colName+' as varchar(200)),''0'')+'''''',''+'
--SET @stringData=@stringData+'''convert(datetime,'+'''+isnull('''''+'''''+convert(varchar(200),'+@colName+',121)+'''''+''''',''NULL'')+'',121),''+'
SET @stringData=@stringData+''''+'''+isnull('''''+'''''+convert(varchar(200),'+@colName+')+'''''+''''',''NULL'')+'',''+'
END
SET @string='INSERT '+@schemaType+'.'+@tableName+'('
SET @string=@string+@colName+','

FETCH NEXT FROM cursCol INTO @colName,@dataType,@schemaType
END
DECLARE @Query nvarchar(4000)
 
SET @query ='SELECT '''+substring(@string,0,len(@string)) + ') VALUES(''+ ' + substring(@stringData,0,len(@stringData)-2)+'''+'')'' FROM '+@schemaType+'.'+@tableName
Print @query
exec sp_executesql @query
 
--select @query
 
CLOSE cursCol
DEALLOCATE cursCol
 

GO
GeneralRe: fine Script, updated with schema of tablememberfutebolb29 Sep '10 - 9:04 
thank you for this update!
GeneralPerfect!!!!memberPatrick_SURFThru4 Feb '09 - 3:10 
Just what I was looking for to make upload data in GoDaddy easier, since they do not allow you to connect to SQL Server from a local enterprise manager.
 
Great work!!!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 17 Jan 2005
Article Copyright 2003 by Sumit Amar .
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid