Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Generate Insert Statements Easily Expand / Collapse
Author
Message
Posted Tuesday, September 25, 2007 9:33 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, September 21, 2002 12:00 AM
Points: 20, Visits: 1
Comments posted to this topic are about the item Generate Insert Statements Easily


Post #402636
Posted Monday, April 14, 2008 8:07 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, April 15, 2008 1:58 AM
Points: 1, Visits: 2
SELECT @exec_str = "SELECT 'INSERT INTO " + @TableName + " VALUES (' + "

line fails to compile in SQL Server Management Studio
Post #484416
Posted Monday, April 14, 2008 4:55 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 11:52 AM
Points: 9,294, Visits: 9,484
Try executing:
SET QUOTED_IDENTIFIER ON
GO

first.

Apparently, the author assumed this setting was already on, though it is not normally on and a bad practice in any event, IMHO.


-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #484702
Posted Tuesday, May 27, 2008 3:30 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Monday, April 14, 2014 6:47 AM
Points: 967, Visits: 450
Even with SET QUOTED_IDENTIFIER ON, it doesnot compile. The query analyzer returns the following error message.

Server: Msg 207, Level 16, State 3, Procedure sp_DataAsInsCommand, Line 27
Invalid column name 'SELECT 'INSERT INTO '.
Server: Msg 207, Level 16, State 1, Procedure sp_DataAsInsCommand, Line 27
Invalid column name ' VALUES (' + '.


Prasad Bhogadi
www.inforaise.com
Post #506768
Posted Tuesday, May 27, 2008 6:55 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 11:52 AM
Points: 9,294, Visits: 9,484
My bad, I got it backwards. Try:
SET QUOTED_IDENTIFIER OFF

instead.


-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #506894
Posted Thursday, June 5, 2008 2:10 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, June 18, 2014 8:58 AM
Points: 24, Visits: 79
Didn't execute for me with either
SET QUOTED_IDENTIFIER OFF
or
SET QUOTED_IDENTIFIER ON

No good reason to use double quotes in this stored proc.



Post #512478
Posted Thursday, June 5, 2008 7:04 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 11:52 AM
Points: 9,294, Visits: 9,484
louis315 (6/5/2008)
Didn't execute for me with either
SET QUOTED_IDENTIFIER OFF
or
SET QUOTED_IDENTIFIER ON

No good reason to use double quotes in this stored proc.


"SET QUOTED_IDENTIFIER OFF" works fine for me. What error do you get now?


-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #512620
Posted Thursday, June 5, 2008 8:03 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, June 18, 2014 8:58 AM
Points: 24, Visits: 79
Sorry, I moved on and banged out my own Insert-generating code using Information_Schema views instead of sysobjects and syscolumns.

I needed code to 1) generate conventional Inserts, 2) create and populate a table variable w/ Inserts, or 3) create a resultset by generating one Select stmt per table row, then sticking "UNION" between each Select.

I think the error I got from this guy's proc was "Invalid column name", or something like that. Might have been due to spaces in some of my table's column names...

Thanks for offering to help. I shouldn't have complained a/b this guy's code when I didn't need it anyway.



Post #512626
Posted Thursday, June 5, 2008 8:19 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 11:52 AM
Points: 9,294, Visits: 9,484
Okey dokey

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #512629
Posted Monday, December 29, 2008 11:54 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, October 9, 2014 4:25 AM
Points: 46, Visits: 44
thats how it works for me
alter PROC p_adm_gen_insert
(@TableList varchar (8000)
)
AS
SET NOCOUNT ON
DECLARE @position int, @exec_str varchar (2000), @TableName varchar (50)
DECLARE @name varchar(128), @xtype int, @status tinyint, @IsIdentity tinyint
DECLARE @ColumnList varchar(8000)
SET @ColumnList=''
SELECT @TableList = @TableList + ','
SELECT @IsIdentity = 0
SELECT @position = PATINDEX('%,%', @TableList)
WHILE (@position <> 0)
BEGIN

SELECT @TableName = SUBSTRING(@TableList, 1, @position - 1)
SELECT @TableList = STUFF(@TableList, 1, PATINDEX('%,%', @TableList),'')
SELECT @position = PATINDEX('%,%', @TableList)

SELECT @exec_str = 'DECLARE fetch_cursor CURSOR FOR SELECT a.name, a.xtype, a.status FROM syscolumns a, sysobjects b WHERE a.id = b.id and b.name = ''' + @TableName + ''''
EXEC (@exec_str)
--print @exec_str
OPEN fetch_cursor
FETCH fetch_cursor INTO @name, @xtype, @status
IF (@status & 0x80) <> 0
BEGIN
SELECT @IsIdentity = 1
SELECT 'SET IDENTITY_INSERT ' + @TableName + ' ON'
SELECT 'GO'
END
SET @ColumnList=@ColumnList + @name +','
--SELECT @exec_str = 'SELECT ''INSERT INTO ' + @TableName + ' VALUES ('' + '
SET @exec_str = ''
Select ' -- The table name is: ' + @TableName
--text or ntext
IF (@xtype = 35) OR (@xtype = 99)
SELECT @exec_str = @exec_str + '''"None yet"'''
ELSE

--image
IF (@xtype = 34)
SELECT @exec_str = @exec_str + '"' + '0xFFFFFFFF' + '"'
ELSE

--smalldatetime or datetime
IF (@xtype = 58) OR (@xtype = 61)
SELECT @exec_str = @exec_str + 'Coalesce( + '''''''' + CONVERT(varchar,' + @name + ',109)' + ' + ''''''''' + ',''null'')'
ELSE

--varchar or char or nvarchar or nchar
IF (@xtype = 167) OR (@xtype = 175) OR (@xtype = 231) OR (@xtype = 239)
SELECT @exec_str = @exec_str + 'Coalesce(' + ''''''''' + ' + @name + ' + ''''''''' + ',''null'')'
ELSE

--uniqueidentifier
IF (@xtype = 36)
SELECT @exec_str = @exec_str + ' + Coalesce('''''''' + ' + ' + CONVERT(varchar(255),' + @name + ')' + ' + ''''''''' + ',''null'')'
ELSE

--binary or varbinary
IF (@xtype = 173) OR (@xtype = 165)
SELECT @exec_str = @exec_str + '"' + '0x0' + '"'
ELSE

SELECT @exec_str = @exec_str + 'Coalesce(CONVERT(varchar,' + @name + '), ''null'')'

WHILE @@FETCH_STATUS <> -1
BEGIN
FETCH fetch_cursor INTO @name, @xtype, @status
IF (@@FETCH_STATUS = -1) BREAK
IF (@status & 0x80) <> 0
BEGIN
SELECT @IsIdentity = 1
SELECT 'SET IDENTITY_INSERT ' + @TableName + ' ON'
SELECT 'GO'
END

SET @ColumnList=@ColumnList + @name +','

--text or ntext
IF (@xtype = 35) OR (@xtype = 99)
SELECT @exec_str = @exec_str + ' + '',''' + ' + ''"None yet"'''
ELSE

--image
IF (@xtype = 34)
SELECT @exec_str = @exec_str + ' + '','' + ' + '"' + '0xFFFFFFFF' + '"'
ELSE

--smalldatetime or datetime
IF (@xtype = 58) OR (@xtype = 61)
SELECT @exec_str = @exec_str + ' + '',''' + ' + Coalesce('''''''' + ' + ' + CONVERT(varchar,' + @name + ',109)' + ' + ''''''''' + ',''null'')'
ELSE

--varchar or char or nvarchar or nchar
IF (@xtype = 167) OR (@xtype = 175) OR (@xtype = 231) OR (@xtype = 239)
SELECT @exec_str = @exec_str + ' + '',''' + ' + Coalesce('''''''' + replace(' + @name + ' COLLATE database_default,'''''''','''''''''''') + ''''''''' + ',''null'')'
ELSE

--uniqueidentifier
IF (@xtype = 36)
SELECT @exec_str = @exec_str + ' + '',''' + ' + Coalesce('''''''' + ' + ' + CONVERT(varchar(255),' + @name + ')' + ' + ''''''''' + ',''null'')'
ELSE

--binary or varbinary
IF (@xtype = 173) OR (@xtype = 165)
SELECT @exec_str = @exec_str + ' + '','' + ' + '"' + '0x0' + '"'
ELSE

SELECT @exec_str = @exec_str + ' + '',''' + ' + Coalesce(CONVERT(varchar,' + @name + '), ''null'')'
END

CLOSE fetch_cursor
DEALLOCATE fetch_cursor

SELECT @exec_str = 'SELECT ''INSERT INTO ' + @TableName +'('+substring(@ColumnList,1,len(@ColumnList)-1) + ')
VALUES ('' + ' + @exec_str + '+ '')'' FROM ' + @TableName
EXEC(@exec_str)
--print (@exec_str)
SELECT 'GO'

IF @IsIdentity = 1
BEGIN
SELECT @IsIdentity = 0
SELECT 'SET IDENTITY_INSERT ' + @TableName + ' OFF'
SELECT 'GO'
END
END

--print @ColumnList




Post #626732
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse