how to use throw in following situation.

  • hi,
      -- i want to use throw iin following way , and it is working but the problem is it showing one red mark below throw.
    because this is written in followning link "The statement before the THROW statement must be followed by the semicolon (;) statement terminator."
    i would like to known why it has been wirtten and how to use it in following case.

    https://msdn.microsoft.com/en-us/library/ee677615.aspx
          begin try
    begin tran

     if (1=2)
     begin
      select 1/0
     end
     else
     begin
       THROW 51000, 'The record does not exist.', 1;
     end

    commit tran
    end try
    begin catch
    select ERROR_MESSAGE()
    end catch

    yours sincelrey

  • It works as written, and is correct (except for the missing commit tran/rollback tran). In this case the SSMS intellisense is incorrectly flagging a lack of a ;

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • GilaMonster - Wednesday, February 22, 2017 6:33 AM

    It works as written, and is correct (except for the missing commit tran/rollback tran). In this case the SSMS intellisense is incorrectly flagging a lack of a ;

    i forgot to write rollback,( but i am taking following way of useing throw is correct)

    begin try
    begin tran

    if (1=2)
    begin
    select 1/0
    end
    else
    begin
    THROW 51000, 'The record does not exist.', 1;
    end

    commit tran
    end try
    begin catch
    rollback tran
    select ERROR_MESSAGE()
    end catch

  • Here's the code again, formatted for readability:


    BEGIN TRY
      BEGIN TRAN;

      IF (
        1 = 2
       )
      BEGIN
       SELECT 1 / 0;
      END;
      ELSE
      BEGIN
       THROW 51000, 'The record does not exist.', 1;
      END;

      COMMIT TRAN;
    END TRY
    BEGIN CATCH
      ROLLBACK TRAN;

      SELECT ERROR_MESSAGE();
    END CATCH;

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

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

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