我正在使用 try...catch 指令来处理 SQL Server 中的错误,我需要识别对象(例如遇到错误的字段的代码),以便我可以显示自己的消息而不是默认的系统错误消息.
这是我的存储过程:
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
这是输出:
Cannot insert duplicate key row in object 'dbo.CT_UserSkills' with unique index 'IX_CT_UserSkills'. The duplicate key value is (4, 1).
但实际上我希望在以下错误消息的模式中找到 '%.*ls',系统消息中的代码为 2601:
Cannot insert duplicate key row in object '%.*ls' with unique index '%.*ls'.
The duplicate key value is %ls.
你能指导我找到那个('%.*ls')吗?