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


Auto generate insert Procedure


Auto generate insert Procedure

Author
Message
vietcode
vietcode
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 2
CRATE procedure [dbo].[Auto_Create_Procedure]
@table_name varchar(200)
AS

DECLARE @column_name varchar(30)
DECLARE @column_length int
--DECLARE @table_name varchar(30)
DECLARE @column_string varchar(4000)
DECLARE @value_string varchar(4000)
DECLARE @ROW_COUNT int
DECLARE @data_type varchar(30)

set @ROW_COUNT = 0
set @column_string = ''
set @value_string = ''

--set @table_name ='tblBook'

DECLARE column_cursor CURSOR FOR
select b.name column_name, b.length, c.name data_type
from sysobjects a, syscolumns b, systypes c
where a.id = b.id and b.xtype = c.xtype and a.name = @table_name and c.name <> 'sysname'
order by colorder

-- Print PROCEDURE command

PRINT 'CREATE PROCEDURE Prc_Insert_' + @table_name + '('

OPEN column_cursor

FETCH NEXT FROM column_cursor
INTO @column_name, @column_length, @data_type


WHILE @@FETCH_STATUS = 0
BEGIN
set @ROW_COUNT = @ROW_COUNT + 1
if(@ROW_COUNT <> @@CURSOR_ROWS)
-- IN TUNG THAM SO RA MAN HINH
PRINT ' @' + @column_name + ' ' + dbo.fnc_get_datatype(@data_type, @column_length) + ', '
else
PRINT ' @' + @column_name + ' ' + dbo.fnc_get_datatype(@data_type, @column_length)

--
set @column_string = @column_string + @column_name + ', '
--
set @value_string = @value_string + '@' + @column_name + ', '
FETCH NEXT FROM column_cursor
INTO @column_name, @column_length, @data_type
END

CLOSE column_cursor;
DEALLOCATE column_cursor;

PRINT ')'
PRINT 'AS'
PRINT 'BEGIN'

-- Print Insert command
print ' INSERT INTO ' + @table_name
print ' (' + SUBSTRING(@column_string, 1, LEN(@column_string) - 1 ) + ') '
print ' VALUES'
print ' (' + SUBSTRING(@value_string, 1, LEN(@value_string) - 1 ) + ')'
PRINT 'END'
PRINT 'GO'

Visit this site to reference SQL command
Mike01
Mike01
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1273 Visits: 1522
Not sure what you question is or if you are just providing a script. The script doesn't work. You need to change 'CRATE' to 'CREATE'. Also, dbo.fnc_get_datatype does not exist

For better, quicker answers, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Edward.Powell
Edward.Powell
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 21
Been around for a long time and well used by a lot of people.

http://vyaskn.tripod.com/code/generate_inserts.txt
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