错误处理 - 非结构化

时间:2016-08-18 19:59:50

标签: sql-server error-handling

有人可以告诉你为什么第一笔交易失败而第二笔交易成功,我已经坚持了一段时间了

GO
DECLARE @errnum AS int;
BEGIN TRAN;
SET IDENTITY_INSERT Production.Products ON;
INSERT INTO Production.Products(productid, productname, supplierid,
categoryid, unitprice, discontinued)
VALUES(1, N'Test1: Ok categoryid', 1, 1, 18.00, 0);
SET @errnum = @@ERROR;
IF @errnum <> 0



BEGIN
IF @@TRANCOUNT > 0 ROLLBACK TRAN;
PRINT 'Insert #1 into Production.Products failed with error ' +
CAST(@errnum AS VARCHAR);
END;
INSERT INTO Production.Products(productid, productname, supplierid,
categoryid,unitprice, discontinued)
VALUES(101, N'Test2: Bad categoryid', 1, 1, 18.00, 0);
SET @errnum = @@ERROR;
IF @errnum <> 0
BEGIN
IF @@TRANCOUNT > 0 ROLLBACK TRAN;
PRINT 'Insert #2 into Production.Products failed with error ' +
CAST(@errnum AS VARCHAR);
END;
SET IDENTITY_INSERT Production.Products OFF;

0 个答案:

没有答案