How can I run SQL commands conditionally?

  • Hello - thanks in advance for any help you can provide.

    I have a situation where I would like to run INSERT SQL commands conditionally. Meaning, if there is data in table A, insert them to table Z, otherwise if there is data from table B, insert them to table Z, otherwise if data in table C, insert them to table Z. I cannot simply execute the INSERT statements sequentially - they can only be run conditionally after first looking in table A, then table B then table C. The destination is always the same - table Z.

    I have been scouring the internet looking for ways to do this within a Script Task. Is this the right approach, to write a Script Task which looks for not EOF within tables before executing SQL? Is it possible to execute SQL statements within Script Tasks - I haven't found good examples of this either? I'm looking for a general approach here - it would help me a lot.

    Thanks!

  • within a single script task you could use the if exists condition and then perform an insert if records exist from that table

    if exists (select tableid from tablea)

    begin

    ...

    end

    else

    if exists...

    Otherwise you could use a script task with a condition on the output. Select 1 record into a variable in ssis, depending on the value of the variable - then branch one way or the other.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

Viewing 2 posts - 1 through 2 (of 2 total)

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