I'm using try...catch instructions to handle errors in SQL Server and I need to recognize the object (e.g. code of the field that has been faced with error) so that I can show my own message instead of default system error message.
Here is my stored procedure:
USE [ArbitraryDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[CTInsert_UserSkills]
(
@UserID_int int, @SkillSN_int int, @Ordering_tinyint tinyint,
@SkillLevelSN_tinyint tinyint, @SkillYearcount_tinyint tinyint
)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @ErrorMessage varchar(200), @ErrorCode int
BEGIN TRY
INSERT INTO CT_UserSkills (UserID_int, SkillSN_int, Ordering_tinyint,
SkillLevelSN_tinyint, SkillYearcount_tinyint)
VALUES (@UserID_int, @SkillSN_int, @Ordering_tinyint, @SkillLevelSN_tinyint,
@SkillYearcount_tinyint)
END TRY
BEGIN CATCH
SELECT ERROR_MESSAGE()
END CATCH
END
and here is the output:
Cannot insert duplicate key row in object 'dbo.CT_UserSkills' with unique index 'IX_CT_UserSkills'. The duplicate key value is (4, 1).
but actually I'm looking to find '%.*ls' in pattern of below error message with code 2601 in sysmessages:
Cannot insert duplicate key row in object '%.*ls' with unique index '%.*ls'.
The duplicate key value is %ls.
Would you please guiding me on finding that ('%.*ls')?