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


create an insert statement


create an insert statement

Author
Message
Jerrin
Jerrin
SSC-Enthusiastic
SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)

Group: General Forum Members
Points: 166 Visits: 52
Comments posted to this topic are about the item create an insert statement
Michael Wasson
Michael Wasson
SSC Rookie
SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)

Group: General Forum Members
Points: 30 Visits: 7
It doesn't generate a complete insert statement, just the first part. "Insert into tablename(columns...)"



Also, this functionality is already built into SQL Query Analyzer.
Michael Wasson
Michael Wasson
SSC Rookie
SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)

Group: General Forum Members
Points: 30 Visits: 7
It doesn't generate a complete insert statement, just the first part. 'Insert into tablename(columns...)'



Also, this functionality is already built into SQL Query Analyzer.
Michael Wasson
Michael Wasson
SSC Rookie
SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)

Group: General Forum Members
Points: 30 Visits: 7
It doesn't generate a complete insert statement, just the first part.



Also, this functionality is already built into SQL Query Analyzer.
Michael Wasson
Michael Wasson
SSC Rookie
SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)

Group: General Forum Members
Points: 30 Visits: 7
It doesn't generate a complete insert statement, just the first part.



Also, this functionality is already built into SQL Query Analyzer.
nigel.
nigel.
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1833 Visits: 2859
Doesn't need to use a cursor, it can be replaced by:



SELECT @InsertQuery = @InsertQuery + column_name + ',' from ##Temp_Generate



Hope Jeff M doesn't see this Wink

--
Nigel
Useful Links:
How to post data/code on a forum to get the best help
The "Numbers" or "Tally" Table - Jeff Moden

Prasad Bhogadi
Prasad Bhogadi
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: 1297 Visits: 450
Yes, its a good suggestion you don't really need to have a cursor to build the insert script. Also if it can be modified to build the script for the complete table if ordinal position is not specified, it would be more handy.

Prasad Bhogadi
www.inforaise.com
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)

Group: General Forum Members
Points: 86428 Visits: 41098
Nigel Rutter (10/4/2007)
Doesn't need to use a cursor, it can be replaced by:



SELECT @InsertQuery = @InsertQuery + column_name + ',' from ##Temp_Generate



Hope Jeff M doesn't see this Wink




Heh... I got'cha covered, Nigel. BigGrin And almost perfect... my only question would be, what are you going to do with the trailing comma Wink

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)

Group: General Forum Members
Points: 86428 Visits: 41098
Thought I'd post something folks can play with... could easily be turned into a proc... can be easily modified for an Insert/Values instead of an Insert/Select. With a little forethought, someone could change it to build "CRUD" for a table. All could be modified to ignore auto-fill columns like TimeStamp data-types and IDENTITY columns.



As a side bar, I'm wondering why someone would take the time to build a code generator like the OP and miss the golden opportunity to format the code that's being output instead of genning the same old garbage that the Microsoft template puts out.



... and no bloody Global Temp table or cursor...





--===== Change to an existing database to test this code in

    USE NorthWind

GO

--===== Build an Insert/Select for the given table

DECLARE @TableName SYSNAME

    SET @TableName = 'dbo.Employees' --<<<< PUT TABLE NAME HERE

DECLARE @Insert VARCHAR(8000)

DECLARE @Select VARCHAR(8000)

DECLARE @MaxNameLength INT



--===== Presets

    SET NOCOUNT ON

    SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED



 SELECT @MaxNameLength = MAX(LEN(sc.Name))

   FROM dbo.SYSCOLUMNS sc,

        dbo.SYSOBJECTS so

  WHERE sc.ID = so.ID

    AND so.ID = OBJECT_ID(@TableName)



--===== Makes the INSERT portion of the code

 SELECT @Insert = ISNULL(@Insert+','+CHAR(10),'') + SPACE(8)+sc.Name

   FROM dbo.SYSCOLUMNS sc,

        dbo.SYSOBJECTS so

  WHERE sc.ID = so.ID

    AND so.ID = OBJECT_ID(@TableName)

  ORDER BY sc.ColID



--===== Makes the SELECT portion of the code

 SELECT @Select = ISNULL(@Select+','+CHAR(10),'') 

                + SPACE(8) + sc.Name + SPACE(@MaxNameLength-LEN(sc.Name)) 

                + ' = xxxx.' + sc.Name

   FROM dbo.SYSCOLUMNS sc,

        dbo.SYSOBJECTS so

  WHERE sc.ID = so.ID

    AND so.ID = OBJECT_ID(@TableName)

  ORDER BY sc.ColID



--===== Display the INSERT/SELECT (Copy from Text Results Window)

 SELECT ' INSERT INTO '+@TableName+CHAR(10)+SPACE(8)+'('+CHAR(10)+@Insert+CHAR(10)+SPACE(8)+')'

 UNION ALL

 SELECT ' SELECT' + CHAR(10) + @Select

 UNION ALL

 SELECT '   FROM sometablename xxxx'

 UNION ALL

 SELECT '  WHERE someconditions'





--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
nigel.
nigel.
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1833 Visits: 2859
I think in the original script the trailing comma was trimmed off after.

--
Nigel
Useful Links:
How to post data/code on a forum to get the best help
The "Numbers" or "Tally" Table - Jeff Moden

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