SQLUSA.com
SQL SERVER 2008 GRAND SLAM
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL JOBS SQL Server Training Scripts JOB BANK
  SQL Server 2008 Training Scripts  
  SQL Server 2005 Training Scripts  
News SQL Server Articles SQL Format

How to catch duplicate insert with TRY-CATCH?

Execute the following SQL Server T-SQL example scripts in Management Studio Query Editor to create and test a stored procedure with duplicate insert try-catch.

-- SQL duplicate insert error handling - SQL Server try - catch

-- SQL stored procedure - SQL duplicate error logging

USE pubs;

GO

CREATE TABLE ErrorLogForTransactions

          (UserName sysname,

           TableName sysname,

           ErrorNumber sysname,

           ErrorSeverity sysname,

           ErrorState sysname,

           ErrorMessage nvarchar(1024),

           ModifiedDate datetime default (getdate()))

GO

          

CREATE PROC procInsertNewBookTitle(

           @TitleID VARCHAR(6),

           @NewTitle NVARCHAR(128),

           @Price    MONEY)

AS

  DECLARE  @TableName    SYSNAME,

           @ErrorMessage NVARCHAR(1024)

  

  BEGIN TRY

    BEGIN TRANSACTION

    

    SELECT @ErrorMessage = 'Duplicate insert failed',

           @TableName = 'Titles'

    

    INSERT dbo.Titles

          (Title_id, Title,  Price)

    VALUES(@TitleID, @NewTitle,@Price)

    

    COMMIT TRANSACTION

  END TRY

  

  BEGIN CATCH

    ROLLBACK TRANSACTION

    

    -- Log the error into transaction error logging table

    INSERT dbo.ErrorLogForTransactions

          (UserName,

           TableName,

           ErrorNumber,

           errorSeverity,

           errorState,

           ErrorMessage)

    VALUES(suser_sname(),@TableName,ERROR_NUMBER(),ERROR_SEVERITY(),

                    ERROR_STATE(),ERROR_MESSAGE())

    -- T-SQL raiserror to notify application about error

    RAISERROR (@ErrorMessage,16,1)

  END CATCH

GO

 

-- Test stored procedure

-- Execute stored procedure

-- Select * from titles

SELECT TOP 1 title_id from titles  -- get test data for duplicate test

-- PC1035

EXEC procInsertNewBookTitle 'PC1035', 'Is Your Webpage User Friendly?', 20.0

GO

/* Messages

 

(0 row(s) affected)

 

(1 row(s) affected)

Msg 50000, Level 16, State 1, Procedure procInsertNewBookTitle, Line 36

Duplicate insert failed

*/

    

SELECT * FROM ErrorLogForTransactions

GO

/* Results

UserName    TableName   ErrorNumber ErrorSeverity     ErrorState  ErrorMessage      ModifiedDate

DELLSTAR\Mary Smith     Titles      2627  14    1     Violation of PRIMARY KEY constraint 'UPKCL_titleidind'. Cannot insert duplicate key in object 'dbo.titles'. 2009-02-21 04:06:29.047

*/

------------

Order SQL 2008 GRAND SLAM Today!
SQLUSA.com Home Page
SQL Server Training at www.sqlusa.com.
SQL Server 2008 Video Training at www.sqlusa.com.
SQL Server 2005 Training Videos at www.sqlusa.com.
Microsoft SQL Server 2000 Training Videos at www.sqlusa.com.

FREE SQL & Business Intelligence / OLAP Short Videos on YOUTUBE.com

Microsoft Community Contributor 2011
Invest in Your SUCCESS!

Search SQLUSA FREE SQL Server Articles & FREE T-SQL Scripts


Copyright 2005-2011, SMI Corp. All Rights Reserved.

SQL Server 2012 is a program product of Microsoft Corporation.
SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.