Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Data script generator


Data script generator

Author
Message
JacekO
JacekO
SSC-Addicted
SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)

Group: General Forum Members
Points: 489 Visits: 606
Comments posted to this topic are about the item Data script generator

---------------------------------------------
Nothing is impossible.
It is just a matter of time and money.

Carlo Romagnano
Carlo Romagnano
Hall of Fame
Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)

Group: General Forum Members
Points: 3606 Visits: 3235
-- This proc print a statement to copy data from one table to another with same structure. e.g. transfering data between servers (production to develop)
-- I use it in SSMS associated with hot key ctrl+3. Select a table name and press ctrl+3.
CREATE PROC sp_insert(@table varchar(257),@alias varchar(128) = 'a')
AS
-- author Carlo Romagnano
DECLARE
@s varchar(8000)
,@i varchar(8000)
,@comma varchar(10)
,@iden INT

SET @alias = ISNULL(@alias,'a')
SET @s = 'SELECT ' + char(13) + char(10)
SET @i = 'INSERT INTO ' + @table + ' (' + char(13) + char(10)
SET @comma = char(9) + ' '
SET @iden = 0

IF LEFT(@TABLE,1) = '#'
SELECT @s = @s + @comma + @alias + '.' + c.name + CASE c.isnullable WHEN 1 THEN ' --NULLABLE' ELSE '' END + CASE WHEN c.cdefault <> 0 THEN ' --DEFAULTED' ELSE '' END
,@i = @i + @comma + c.name + CASE c.isnullable WHEN 1 THEN ' --NULLABLE' ELSE '' END + CASE WHEN c.cdefault <> 0 THEN ' --DEFAULTED' ELSE '' END
,@comma = char(13) + char(10) + char(9) + ','
,@iden = @iden + COLUMNPROPERTY (c.id, c.name, 'IsIdentity')
from tempdb.dbo.syscolumns c
where c.id = object_id('tempdb.dbo.' + @table)
AND c.name <> 'timestamp'
AND c.iscomputed = 0
order by c.colorder
ELSE
SELECT @s = @s + @comma + @alias + '.' + c.name + CASE c.isnullable WHEN 1 THEN ' --NULLABLE' ELSE '' END + CASE WHEN c.cdefault <> 0 THEN ' --DEFAULTED' ELSE '' END
,@i = @i + @comma + c.name + CASE c.isnullable WHEN 1 THEN ' --NULLABLE' ELSE '' END + CASE WHEN c.cdefault <> 0 THEN ' --DEFAULTED' ELSE '' END
,@comma = char(13) + char(10) + char(9) + ','
,@iden = @iden + COLUMNPROPERTY (c.id, c.name, 'IsIdentity')
from syscolumns c
where c.id = object_id(@table)
AND c.name <> 'timestamp'
AND c.iscomputed = 0
order by c.colorder

if @iden > 0
print 'SET IDENTITY_INSERT ' + @table + ' ON'
print @i + char(13) + char(10) + ')'
print @s + char(13) + char(10) + ' FROM ' + @table + ' ' + @alias

if @iden > 0
print 'SET IDENTITY_INSERT ' + @table + ' OFF'
JacekO
JacekO
SSC-Addicted
SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)

Group: General Forum Members
Points: 489 Visits: 606
The difference between your script and mine is the usage and the output it generates. The objective of my script is to generate a set of insert statements that can be stored in a file and taken offline or to another, not linked server.

---------------------------------------------
Nothing is impossible.
It is just a matter of time and money.

ramu.valleti
ramu.valleti
Old Hand
Old Hand (381 reputation)Old Hand (381 reputation)Old Hand (381 reputation)Old Hand (381 reputation)Old Hand (381 reputation)Old Hand (381 reputation)Old Hand (381 reputation)Old Hand (381 reputation)

Group: General Forum Members
Points: 381 Visits: 340
Hello there,

when i try to execute the procedure its throwing the following error,

" Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'. "

Can any one help me what is the problem?

Regards,
Ramu

Ramu
No Dream Is Too Big....!
JacekO
JacekO
SSC-Addicted
SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)

Group: General Forum Members
Points: 489 Visits: 606
Replace the VARCHAR declarations with NVARCHAR and let me know if it helped.

---------------------------------------------
Nothing is impossible.
It is just a matter of time and money.

Linson.Daniel
Linson.Daniel
Old Hand
Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)Old Hand (315 reputation)

Group: General Forum Members
Points: 315 Visits: 391
Well Jack,

You might wanna have a look at this stored procedure that I had written a few months back......similar to what you were trying to achieve......

http://www.sqlservercentral.com/scripts/insert+script+generator/65407/

Using the above sp that I have written you could generate for multiple tables all at once , just pass the table names in a concatenated fashion......and oh by way it also supports all datatypes i.e to say the insert script would be generated taking into consideration the datatypes......except for the sql_variant datatype.......

ohh did I mention it cares of the single quotes too.......Cool

Cheers
Linson Daniel
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