• 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...

    [font="Courier New"]

    --=====&nbspChange&nbspto&nbspan&nbspexisting&nbspdatabase&nbspto&nbsptest&nbspthis&nbspcode&nbspin

    &nbsp&nbsp&nbsp&nbspUSE&nbspNorthWind

    GO

    --=====&nbspBuild&nbspan&nbspInsert/Select&nbspfor&nbspthe&nbspgiven&nbsptable

    DECLARE&nbsp@TableName&nbspSYSNAME

    &nbsp&nbsp&nbsp&nbspSET&nbsp@TableName&nbsp=&nbsp'dbo.Employees'&nbsp--<<<<&nbspPUT&nbspTABLE&nbspNAME&nbspHERE

    DECLARE&nbsp@Insert&nbspVARCHAR(8000)

    DECLARE&nbsp@Select&nbspVARCHAR(8000)

    DECLARE&nbsp@MaxNameLength&nbspINT

    --=====&nbspPresets

    &nbsp&nbsp&nbsp&nbspSET&nbspNOCOUNT&nbspON

    &nbsp&nbsp&nbsp&nbspSET&nbspTRANSACTION&nbspISOLATION&nbspLEVEL&nbspREAD&nbspUNCOMMITTED

    &nbspSELECT&nbsp@MaxNameLength&nbsp=&nbspMAX(LEN(sc.Name))

    &nbsp&nbsp&nbspFROM&nbspdbo.SYSCOLUMNS&nbspsc,

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspdbo.SYSOBJECTS&nbspso

    &nbsp&nbspWHERE&nbspsc.ID&nbsp=&nbspso.ID

    &nbsp&nbsp&nbsp&nbspAND&nbspso.ID&nbsp=&nbspOBJECT_ID(@TableName)

    --=====&nbspMakes&nbspthe&nbspINSERT&nbspportion&nbspof&nbspthe&nbspcode

    &nbspSELECT&nbsp@Insert&nbsp=&nbspISNULL(@Insert+','+CHAR(10),'')&nbsp+&nbspSPACE(8)+sc.Name

    &nbsp&nbsp&nbspFROM&nbspdbo.SYSCOLUMNS&nbspsc,

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspdbo.SYSOBJECTS&nbspso

    &nbsp&nbspWHERE&nbspsc.ID&nbsp=&nbspso.ID

    &nbsp&nbsp&nbsp&nbspAND&nbspso.ID&nbsp=&nbspOBJECT_ID(@TableName)

    &nbsp&nbspORDER&nbspBY&nbspsc.ColID

    --=====&nbspMakes&nbspthe&nbspSELECT&nbspportion&nbspof&nbspthe&nbspcode

    &nbspSELECT&nbsp@Select&nbsp=&nbspISNULL(@Select+','+CHAR(10),'')&nbsp

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp+&nbspSPACE(8)&nbsp+&nbspsc.Name&nbsp+&nbspSPACE(@MaxNameLength-LEN(sc.Name))&nbsp

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp+&nbsp'&nbsp=&nbspxxxx.'&nbsp+&nbspsc.Name

    &nbsp&nbsp&nbspFROM&nbspdbo.SYSCOLUMNS&nbspsc,

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspdbo.SYSOBJECTS&nbspso

    &nbsp&nbspWHERE&nbspsc.ID&nbsp=&nbspso.ID

    &nbsp&nbsp&nbsp&nbspAND&nbspso.ID&nbsp=&nbspOBJECT_ID(@TableName)

    &nbsp&nbspORDER&nbspBY&nbspsc.ColID

    --=====&nbspDisplay&nbspthe&nbspINSERT/SELECT&nbsp(Copy&nbspfrom&nbspText&nbspResults&nbspWindow)

    &nbspSELECT&nbsp'&nbspINSERT&nbspINTO&nbsp'+@TableName+CHAR(10)+SPACE(8)+'('+CHAR(10)+@Insert+CHAR(10)+SPACE(8)+')'

    &nbspUNION&nbspALL

    &nbspSELECT&nbsp'&nbspSELECT'&nbsp+&nbspCHAR(10)&nbsp+&nbsp@Select

    &nbspUNION&nbspALL

    &nbspSELECT&nbsp'&nbsp&nbsp&nbspFROM&nbspsometablename&nbspxxxx'

    &nbspUNION&nbspALL

    &nbspSELECT&nbsp'&nbsp&nbspWHERE&nbspsomeconditions'

    [/font]

    --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.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)