|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, November 02, 2010 6:52 AM
Points: 158,
Visits: 52
|
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Monday, October 22, 2007 7:30 AM
Points: 11,
Visits: 4
|
|
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.
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Monday, October 22, 2007 7:30 AM
Points: 11,
Visits: 4
|
|
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.
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Monday, October 22, 2007 7:30 AM
Points: 11,
Visits: 4
|
|
It doesn't generate a complete insert statement, just the first part.
Also, this functionality is already built into SQL Query Analyzer.
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Monday, October 22, 2007 7:30 AM
Points: 11,
Visits: 4
|
|
It doesn't generate a complete insert statement, just the first part.
Also, this functionality is already built into SQL Query Analyzer.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Monday, June 03, 2013 9:46 AM
Points: 1,164,
Visits: 2,603
|
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Thursday, May 02, 2013 5:00 AM
Points: 967,
Visits: 447
|
|
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
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 7:18 AM
Points: 33,112,
Visits: 27,038
|
|
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 ;)
Heh... I got'cha covered, Nigel. :D And almost perfect... my only question would be, what are you going to do with the trailing comma ;)
--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."
For better, quicker answers on T-SQL questions, 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/
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 7:18 AM
Points: 33,112,
Visits: 27,038
|
|
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."
For better, quicker answers on T-SQL questions, 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/
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Monday, June 03, 2013 9:46 AM
Points: 1,164,
Visits: 2,603
|
|
|
|
|