Sql server 查找SQL Server try/catch块中遇到错误的objectid
我正在使用try…catch指令来处理SQL Server中的错误,我需要识别对象,例如遇到错误的字段的代码,以便显示我自己的消息,而不是默认的系统错误消息 以下是我的存储过程:Sql server 查找SQL Server try/catch块中遇到错误的objectid,sql-server,exception-handling,try-catch,sysobjects,Sql Server,Exception Handling,Try Catch,Sysobjects,我正在使用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, @S
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
ALTER PROCEDURE [dbo].[CTInsert_UserSkills]
(
@UserID_int int, @SkillSN_int int, @Ordering_tinyint tinyint,
@SkillLevelSN_tinyint tinyint, @SkillYearcount_tinyint tinyint,
@ObjectID int OUTPUT
)
以下是输出:
Cannot insert duplicate key row in object 'dbo.CT_UserSkills' with unique index 'IX_CT_UserSkills'. The duplicate key value is (4, 1).
但实际上,我正在查找sysmessages中代码为2601的以下错误消息模式中的''%1!''
Cannot insert duplicate key row in object '%.*ls' with unique index '%.*ls'.
The duplicate key value is %ls.
请指导我查找'%1!'好吗?向存储过程添加另一个输出参数:
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
ALTER PROCEDURE [dbo].[CTInsert_UserSkills]
(
@UserID_int int, @SkillSN_int int, @Ordering_tinyint tinyint,
@SkillLevelSN_tinyint tinyint, @SkillYearcount_tinyint tinyint,
@ObjectID int OUTPUT
)
然后在catch块中,将此变量设置为需要发送回调用程序的任何值。亲爱的Sam,这并不意味着如何将变量值发送到外部世界,而是如何找到%.*ls。关键在于“需要发送回的任何值”。这是一件很具体的事情,问题是如何找到这个价值。你知道,仅仅因为你不理解答案而投票否决那些试图帮助你的人是非常粗鲁的。