conditionally running a tsql-script

  • Hey,

    I need to run a sql script if a certain condition holds (if a specific table exists). This script mainly creates new tables (after dropping the old ones). After each statement I have the GO keyword. How can I surround the entire script with a conditional clause? I tried the BEGIN...END syntax but it doesn't work. I don't want to check for this condition before each statement.

    Is there a way to do it?

     

    Thanks

  • I don't think you can do this. Go is the batch terminator.

    Can you post the code so we can provide suggestions?

  • Hey,

    Iv'e devised a work-around - it's quite "patchy" but nevertheless achieves its goal:

    I put my entire script in a stored procedure "BUILD_TABLES_SP". Then, I load the SP to the database. Now, all I need is to check if the condition holds and if it does - I execute the stored procedure.

    It works great. Do you have a more elegant solution?

     

    Thanks,

     

    --Eyal

  • Can't provide one without seeing the whole code, sorry.

  • If you need to run the entire sequence every time the condition is fulfilled, then I'd say SP is a good solution, and not at all patchy. Question is, whether GO is necessary after each statement...

  • I don't think Go is necessary after each statement.  I agree SP is the way to go with your conditional.  I check for table existence many times in SPs before I do whatever.  As I have a whole batch of scripts to run each day I then incorporate them into DTS packages and then include those in a scheduled job.  Works a dream.


    Best Regards
    Terry

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

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