調用此代碼的客戶端受到限制,只能處理來自存儲的特定代碼的返回代碼。所以,我們修改我們平常合同RETURN -1
上的錯誤和默認到RETURN 0
如果沒有錯誤SQL Server存儲過程返回代碼奇怪
如果代碼打內catch塊,然後返回碼默認值是-4而不是0
有誰知道這是從哪裏來的?參考
乾杯 GBN
IF OBJECT_ID('dbo.foo') IS NOT NULL DROP TABLE dbo.foo
GO
CREATE TABLE dbo.foo (
KeyCol char(12) NOT NULL,
ValueCol xml NOT NULL,
Comment varchar(1000) NULL,
CONSTRAINT PK_foo PRIMARY KEY CLUSTERED (KeyCol)
)
GO
IF OBJECT_ID('dbo.bar') IS NOT NULL DROP PROCEDURE dbo.bar
GO
CREATE PROCEDURE dbo.bar
@Key char(12),
@Value xml,
@Comment varchar(1000)
AS
SET NOCOUNT ON
DECLARE @StartTranCount tinyint;
BEGIN TRY
SELECT @StartTranCount = @@TRANCOUNT;
IF @StartTranCount = 0 BEGIN TRAN;
BEGIN TRY
--SELECT @StartTranCount = 'fish' --generates an error and goes to outer CATCH
INSERT dbo.foo (KeyCol, ValueCol, Comment) VALUES (@Key, @Value, @Comment);
END TRY
BEGIN CATCH
IF ERROR_NUMBER() = 2627 --PK violation
UPDATE
dbo.foo
SET
ValueCol = @Value, Comment = @Comment
WHERE
KeyCol = @Key;
ELSE
RAISERROR ('Tits up', 16, 1);
END CATCH
IF @StartTranCount = 0 COMMIT TRAN;
END TRY
BEGIN CATCH
IF @StartTranCount = 0 AND XACT_STATE() <> 0 ROLLBACK TRAN;
RETURN -1
END CATCH
--Without this, we'll send -4 if we hit the UPDATE CATCH block above
--RETURN 0
GO
--please run these **separately**
--Run with RETURN 0 and fish line commented out
DECLARE @rtn int
EXEC @rtn = dbo.bar 'abcdefghijkl', '<foobar />', 'testing'
SELECT @rtn; SELECT * FROM dbo.foo
GO
DECLARE @rtn int
EXEC @rtn = dbo.bar 'abcdefghijkl', '<foobar2 />', 'testing2'
--updated OK but we get @rtn = -4
SELECT @rtn; SELECT * FROM dbo.foo
GO
--uncomment fish line
DECLARE @rtn int
EXEC @rtn = dbo.bar 'abcdefghijkl', '<foobar />', 'testing'
--Hit outer CATCH, @rtn = -1 as expected
SELECT @rtn; SELECT * FROM dbo.foo
你爲什麼將「魚」分配給tinyint? – 2010-06-03 10:55:58
強制執行異常以測試外部CATCH塊。看到最後一個EXEC ... – gbn 2010-06-03 11:01:55
抱歉,在我看到您的回覆之前,我已刪除了我的評論。 (我的評論是虛假的:) – 2010-06-03 11:22:33