Run second query if first completes

  • I am having a total brain cramp.

    I have a query the populates a month_end_summary table from nightly_results table.

    If the query runs correctly i want to run a query to delete the nightly_Results table.

    but only if the first summary query runs with out error.

    i can do the try catch on the first to catch an error, but blanking out on how to run the delete query if the try catch succeds.

  • Just execute the second query after the first within your try block.

    If the first statement succeeds, the second statement will execute.

    If the first statement fails, it will jump to the catch block w/o executing the second statement.

    e.g.,

    BEGIN TRY
    SELECT 1/0;-- change me to 1/2 to see successful execution

    EXEC sp_executesql N'SELECT * FROM sys.tables';

    END TRY

    BEGIN CATCH
    THROW; -- Obvviously, you may not want to just rethow the error. Just an illustration

    END CATCH
  • ahhhh stupid me. It was a long weekend

  • can we use the same method in databricks to create group, since for creating workspace local group it doesn't  work as ' create group if not exists group_name '  i'm having trouble for a month to do that. kindly assist me. THANK YOU!

    as below command?

    BEGIN TRY

    CREATE GROUP group_name;

    END TRY

    BEGIN CATCH

    ALTER GROUP group_name ADD USER user@el.com;

    END CATCH

  • buvana wrote:

    can we use the same method in databricks to create group, since for creating workspace local group it doesn't  work as ' create group if not exists group_name '  i'm having trouble for a month to do that. kindly assist me. THANK YOU!

    as below command?

    BEGIN TRY CREATE GROUP group_name;

    END TRY

    BEGIN CATCH ALTER GROUP group_name ADD USER user@el.com;

    END CATCH

    Look at your logic.  If the create group works, there will be no error and so the Add User will never be executed.

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

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

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