G'Day,

I am using SQL Server 2K and was just wondering if someone had a more
elegant way for me to handle errors. Below is a sample stored procedure
showing how I am trapping errors. As you can see it is rather messy and
requires a GOTO once we get an error. Is there a more elegant way to do
this? Obviously a try....except mechanism would be great!!! ;-)

Any help, pointers, suggestions etc. greatly appreciated.....

TIA

CREATE PROCEDURE [dbo].[sp_MyProc]
AS
  DECLARE @Err Int
  BEGIN TRANSACTION

  INSERT INTO tblMyTable
  VALUES ('A', 'B')
  SELECT @Err = @@ERROR
  IF @Err <> 0
  BEGIN
        -- Error
        GOTO error
  END ELSE
  BEGIN
        -- Success
        -- Do something in here blah blah
  END

  INSERT INTO tblMyOtherTable
  VALUES ('A', 'B')
  SELECT @Err = @@ERROR
  IF @Err <> 0
  BEGIN
        -- Error
        GOTO error
  END ELSE
  BEGIN
        -- Success
        -- Do something in here blah blah
  END

  COMMIT TRANSACTION
-- Return Success
RETURN @Err
/*  Error Handling  */
error:
      ROLLBACK TRANSACTION
      RETURN @Err

GO

-- Donovan
----------------------------------------------------------------------
Donovan J. Edye [www.edye.wattle.id.au]
Namadgi Systems [www.namsys.com.au]
Voice: +61 2 6285-3460
Fax: +61 2 6285-3459
TVisualBasic = Class(None);
Heard just before the 'Big Bang': "...Uh Oh...."
----------------------------------------------------------------------
GXExplorer [http://www.gxexplorer.org] Freeware Windows Explorer
replacement. Also includes freeware delphi windows explorer components.
----------------------------------------------------------------------


---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED] 
with body of "unsubscribe delphi"

Reply via email to