How to store a SQL statement in a table?

  • I'm needing to store a SQL statement in a VARCHAR(MAX) column in a database table, however I'm having trouble getting statemen to insert. What is the best aproach to inserting SQL statements into a DB? Im working with somthing like what I have listed below..

    INSERT INTO [DATABASE].[dbo].[TableName]

    VALUES ('UPDATE DATABASE.TABLE

    SET COLUMNNAME = '1000'

    WHERE IDENTIFIER IN('P65299' , 'Z5457' , '32657A'))

    Thanks!

  • Code-1029433 (3/28/2013)


    I'm needing to store a SQL statement in a VARCHAR(MAX) column in a database table, however I'm having trouble getting statemen to insert. What is the best aproach to inserting SQL statements into a DB? Im working with somthing like what I have listed below..

    INSERT INTO [DATABASE].[dbo].[TableName]

    VALUES ('UPDATE DATABASE.TABLE

    SET COLUMNNAME = '1000'

    WHERE IDENTIFIER IN('P65299' , 'Z5457' , '32657A'))

    Thanks!

    I would say don't do it. Create a stored proc, a sql job, anything. Using this is going to be painful in the long run. You will have to query the database to get the query. Stick the results into a nvarchar and then execute the contents.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Code-1029433 (3/28/2013)


    I'm needing to store a SQL statement in a VARCHAR(MAX) column in a database table, however I'm having trouble getting statemen to insert. What is the best aproach to inserting SQL statements into a DB? Im working with somthing like what I have listed below..

    INSERT INTO [DATABASE].[dbo].[TableName]

    VALUES ('UPDATE DATABASE.TABLE

    SET COLUMNNAME = '1000'

    WHERE IDENTIFIER IN('P65299' , 'Z5457' , '32657A'))

    Thanks!

    Try this, but I agree with Sean:

    INSERT INTO [DATABASE].[dbo].[TableName]

    VALUES ('UPDATE DATABASE.TABLE

    SET COLUMNNAME = ''1000''

    WHERE IDENTIFIER IN(''P65299'' , ''Z5457'' , ''32657A'')')

  • Code-1029433 (3/28/2013)


    I'm needing to store a SQL statement in a VARCHAR(MAX) column in a database table, however I'm having trouble getting statemen to insert. What is the best aproach to inserting SQL statements into a DB? Im working with somthing like what I have listed below..

    INSERT INTO [DATABASE].[dbo].[TableName]

    VALUES ('UPDATE DATABASE.TABLE

    SET COLUMNNAME = '1000'

    WHERE IDENTIFIER IN('P65299' , 'Z5457' , '32657A'))

    Thanks!

    Normally I would just use a SP, but this place I just started working at has a program that pulls TSQL statements from this SQL database to perform maintainace tasks on Mainframe systems, and then updates the row in the SQL DB once the task has complted with historical transaction info related to the SQL script that was used. Not sure why they decided to do it that way, but it is what it is .

  • Thanks Lynn!

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply