SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Scripting INSERTs


Scripting INSERTs

Author
Message
Tony Webster
Tony Webster
SSC Veteran
SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)

Group: General Forum Members
Points: 250 Visits: 321
Comments posted to this topic are about the item Scripting INSERTs
Anipaul
Anipaul
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12563 Visits: 1407
Nice article...



Adam Tappis
Adam Tappis
Valued Member
Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)

Group: General Forum Members
Points: 54 Visits: 97
Nicely done. However I would look to apply the the filter condition as early as possible, because on a 1 million row table, the procedure will cursor over all 1 million rows even if the condition limits the result to just 3 rows...
Mike Sofen-356112
Mike Sofen-356112
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 59
Suggested revisions: eliminate the cursor with a "while" loop, and use the metadata more effectively to eliminate the function calls. The input param that is nullable simply needs the standard "= null", rather than " = '1=1' ". Use "set nocount on" at the beginning of the sproc to eliminate the bazillion return messages this sproc generates.

And the biggies: you're taking the top 100 columns from a table. That means, if a table has 107 columns, then you'll only script 100 of them...does that make any sense? If indeed there is an obscure sql fault on greater than 100 columns, then skip the table and return a message to the user saying just that. Ditto for the text column...don't partially script tables. Also, you strangely AREN'T filtering for calculated columns, which you cannot insert values for...those columns need to be remove from the #Fields table.

Mike Sofen
San Diego, CA
grant.owens
grant.owens
SSC Journeyman
SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)

Group: General Forum Members
Points: 93 Visits: 232
Nice stuff and very useful. There is a more sophisticated version of this at http://vyaskn.tripod.com/code.htm#inserts that i've used fairly extensively.
Etni
Etni
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 Visits: 120
google sp_generate_inserts
dfarran
dfarran
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 150
Nice article. You can actually drop the cursor completely and replace with the following


DECLARE @fields nvarchar(4000)
DECLARE @values nvarchar(4000)
SET @fields = ''
SET @values = ''
SELECT @fields = @fields + ',[' + sc.name + ']' ,
@values = @values + '+'',''+dbo.'
+ CASE WHEN st.name = 'money' THEN 'spMVal'
ELSE 'spVal'
END
+ '([' + sc.name + '])'
FROM syscolumns sc
JOIN sysobjects so ON so.id = sc.id
JOIN systypes st ON st.xusertype = sc.xusertype
WHERE so.name = @table
AND st.name <> 'text'
ORDER BY sc.colorder

PRINT @fields
PRINT @values




In SQL 2000 there is the (mentioned in the caveats) issue with nvarchar(4000) meaning you will potentially run out of string space, this is much less of an issue in 2005 with nvarchar(MAX).

I hope someone finds this SELECT @str = @str + ... useful, I know I have. I have found some occasions where it only returns one line, but if you include TOP 100 percent it will generally solve this issue.
Tony Webster
Tony Webster
SSC Veteran
SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)SSC Veteran (250 reputation)

Group: General Forum Members
Points: 250 Visits: 321
That means, if a table has 107 columns, then you'll only script 100 of them...does that make any sense? If indeed there is an obscure sql fault on greater than 100 columns, then skip the table and return a message to the user saying just that. Ditto for the text column...don't partially script tables. Also, you strangely AREN'T filtering for calculated columns, which you cannot insert values for...those columns need to be remove from the #Fields table.


All good points - this was never indented to be complete to that degree. What actually is more likely to happen before you hit 100 columns I think is that the varchar variable used to assemble the SQL would overflow its length.

I don't actually run what it produces without checking it visually first. All I was after was something simple to fulfill a particular need - a plank to get over the stream at the bottom of the garden - a fully stress-tested suspension bridge it certainly isn't! That said, there are some good ideas here and elsewhere in the thread for improving it relatively simply.
Alexander Kuznetsov
Alexander Kuznetsov
SSC Eights!
SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)

Group: General Forum Members
Points: 977 Visits: 824
I've been using sp_generate_inserts for a long time, no problems:

http://vyaskn.tripod.com/code/generate_inserts.txt
Yelena Varshal
Yelena Varshal
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14602 Visits: 607
Why Insert... Select would not do?


Regards,
Yelena Varshal

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search