|
|
Comments and Discussions
|
|
 |

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

|
The procedure works fine. Thanks for sharing.
|
|
|
|

|
Thanks for the effort! works for me!
|
|
|
|
|

|
This worked perfectly for me, thanks a million
|
|
|
|

|
i would say AWESOME dude
thanks man great job.
|
|
|
|

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

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

|
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') BEGIN
SET @stringData=@stringData+'
isnull(''''''''+REPLACE(cast('+@colName+' as varchar(2000)),'''''''','''''''''''')+'''''''',''NULL'')+'',''+'
END
|
|
|
|

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

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

|
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[^]
|
|
|
|
|

|
Thanks a lot.
modified 7 Feb '12 - 7:29.
|
|
|
|

|
This was exactly what I was looking for and worked just as I hoped.
|
|
|
|

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

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

|
Well done!! Very efficient coding!!
|
|
|
|
|

|
Very nice article, its really helped me and saved my time
|
|
|
|

|
just what i needed, i had the colname edited to have [] in case the table design had reserved words. many thanks
|
|
|
|

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

|
Hi again, this has been a wrestle, the quoting could send you over the edge on a bad day
here's the whole thing - which copes properly with very large xml data blocks:-
USE [******]
GO
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 cursCol CURSOR FAST_FORWARD FOR
SELECT column_name,data_type FROM information_schema.columns WHERE table_name = @tableName
OPEN cursCol
DECLARE @string nvarchar(max) DECLARE @stringData nvarchar(max) DECLARE @dataType nvarchar(max) 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
IF @dataType in ('varchar','char','nchar','nvarchar')
BEGIN
SET @stringData=@stringData+''''+'''+isnull('''''+'''''+'+@colName+'+'''''+''''',''NULL'')+'',''+'
END
ELSE
if @dataType in ('text','ntext') BEGIN
SET @stringData=@stringData+'''''''''+isnull(cast('+@colName+' as varchar(2000)),'''')+'''''',''+'
END
ELSE
IF @dataType = 'money' BEGIN
SET @stringData=@stringData+'''convert(money,''''''+isnull(cast('+@colName+' as varchar(200)),''0.0000'')+''''''),''+'
END
ELSE
IF @dataType='datetime'
BEGIN
SET @snippet='''convert(datetime,'+'''+isnull('''''+'''''+convert(varchar(200),'+@colName+',121)+'''''+''''',''NULL'')+'',121),''+'
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)),'''''''','''''''''''')+'''''''',+'
SET @stringData = @stringData+@snippet
END
ELSE BEGIN
SET @stringData=@stringData+''''+'''+isnull('''''+'''''+convert(varchar(200),'+@colName+')+'''''+''''',''NULL'')+'',''+'
END
SET @string=@string+@colName+','
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
exec sp_executesql @query
CLOSE cursCol
DEALLOCATE cursCol
|
|
|
|

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

|
Excellent, saved me a lot of work, thanks
|
|
|
|

|
Muy buena ayuda para la transportacion de datos entre manejadores
|
|
|
|
|

|
practical and to the point. thanks
|
|
|
|

|
Great script! It appears not to work with temp tables.
|
|
|
|

|
Excellent and very useful script but you need to add an "INTO" qualifier in Line
SET @string='INSERT INTO '+@tableName+'('
|
|
|
|

|
I only tested in SQL Server and INTO clause is not required there.
|
|
|
|

|
Hey dude great solution thanks a lot keep making this type of articles thanks a lot one again..
|
|
|
|
|

|
Very useful. Thanks Sumit.
|
|
|
|
|
|
|
|

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

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

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

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

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

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

|
thank you for this update!
|
|
|
|

|
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 News Suggestion Question Bug Answer Joke Rant Admin
|
Stored procedure to generate INSERT..VALUES statements in SQL Server.
| Type | Article |
| Licence | CPOL |
| First Posted | 6 Dec 2003 |
| Views | 345,177 |
| Bookmarked | 87 times |
|
|