1

我有一个运行一系列插入的存储过程,但在某些情况下,其中一个插入似乎没有运行,但它后面的其余代码运行良好。

DECLARE @ID1 int
DECLARE @ID2 int

-- This works
INSERT INTO table1 (field1,field2)
VALUES('test', 1)
SELECT @ID1 = SCOPE_IDENTITY() -- This picks up the correct ID

-- This sometimes doesn't seem to run or an error of some sort happens infrequently
INSERT INTO table2 (field1, field2, field3, field4, field5)
VALUES(1,2,3,4,@ID1)

-- This always runs, but if the previous insert doesn't run inserts the previous ID into @ID2
SELECT @ID2 = SCOPE_IDENTITY()

-- Inserts the ID from the previous step and therefore is sometimes wrong
INSERT INTO table3 (field1,field2)
VALUES ('testing', @ID2)

这是一个经过编辑的 SP 样本。为什么有时会发生这种情况?我会想如果INSERT剩下的程序有问题会爆炸吗?但相同的代码大部分时间都由同一个人运行。我检查了在问题发生的同时是否有任何表锁定,但似乎没有任何我可以发现的东西。

找出问题所在的最佳方法是什么?或者我应该如何重组上述代码以消除可能的影响?

4

3 回答 3

1

放置TRY / CATCH第二个 INSERT 以查看可能的错误。

于 2015-07-24T08:51:17.257 回答
1

在插入行之后检查 @@Error 以查看是否引发了“静默”错误。您也可以尝试将插入行包装在 TRY/CATCH 块中,但我怀疑这不会有成效,因为我假设您在管理工作室的输出窗口中没有收到任何错误?

尝试在运行存储过程的同时运行 SQL Profiler,以查看它是否提供有关失败的任何进一步信息。

对存储过程的以下更改可能会从 SP 本身为您提供尽可能多的信息,以了解正在发生的事情。它不是完美的代码,但它应该提供任何错误发生和被“吞下”的体面指示:

DECLARE @ID1 int
DECLARE @ID2 int

-- This works
INSERT INTO table1 (field1,field2)
VALUES('test', 1)
SELECT @ID1 = SCOPE_IDENTITY() -- This picks up the correct ID

-- This sometimes doesn't seem to run
IF @@error <> 0
BEGIN
    PRINT 'Error before: ' + (cast @@error as varchar)
END

BEGIN TRY
    INSERT INTO table2 (field1, field2, field3, field4, field5)
    VALUES(1,2,3,4,@ID1)
END TRY
BEGIN CATCH
    SELECT ERROR_NUMBER() AS ErrorNumber
        ,ERROR_SEVERITY() AS ErrorSeverity
        ,ERROR_STATE() AS ErrorState
        ,ERROR_PROCEDURE() AS ErrorProcedure
        ,ERROR_LINE() AS ErrorLine
        ,ERROR_MESSAGE() AS ErrorMessage;
END CATCH

IF @@error <> 0
BEGIN
    PRINT 'Error after: ' + cast(@@error as varchar)
END

-- This always runs, but if the previous insert doesn't run inserts the previous ID into @ID2
SELECT @ID2 = SCOPE_IDENTITY()

-- Inserts the ID from the previous step and therefore is sometimes wrong
INSERT INTO table3 (field1,field2)
VALUES ('testing', @ID2)
于 2015-07-24T08:54:21.790 回答
0

使用 try/catch 并在 catch 语句中引发错误。例如-:

CREATE PROC Usp_InsertBulkRecord
AS
BEGIN
BEGIN TRY
    DECLARE @ID1 int
    DECLARE @ID2 int

-- This works
INSERT INTO table1 (field1,field2)
VALUES('test', 1)
SELECT @ID1 = SCOPE_IDENTITY() -- This picks up the correct ID

-- This sometimes doesn't seem to run
INSERT INTO table2 (field1, field2, field3, field4, field5)
VALUES(1,2,3,4,@ID1)

-- This always runs, but if the previous insert doesn't run inserts the previous ID into @ID2
SELECT @ID2 = SCOPE_IDENTITY()

-- Inserts the ID from the previous step and therefore is sometimes wrong
INSERT INTO table3 (field1,field2)
VALUES ('testing', @ID2)
END TRY              
  BEGIN CATCH              
  DECLARE @Error varchar(8000)                                                            
    SET @Error= Convert(varchar,ERROR_NUMBER()) + '*****' + Convert(varchar(4000),ERROR_MESSAGE())                                                             
    + '*****' + isnull(Convert(varchar,ERROR_PROCEDURE()),'[Usp_InsertBulkRecord]')                     
    + '*****' + Convert(varchar,ERROR_LINE()) + '*****' + Convert(varchar,ERROR_SEVERITY())                         
    + '*****' + Convert(varchar,ERROR_STATE())                                                            
    RAISERROR (@Error,16,1);               
  END CATCH 

END
于 2015-07-24T09:12:05.007 回答