是否可以根据XACT_STATE将数据插入表中。例如:
IF(XACT_STATE() = 1)
BEGIN
INSERT INTO TABLE1 VALUES ()
COMMIT TRANSACTION
END
ELSE
IF(XACT_STATE() = -1)
BEGIN
INSERT INTO TABLE2 VALUES ()
ROLLBACK TRANSACTION
END
SELECT ERROR_NUMBER() AS ERRORNUMBER,
ERROR_SEVERITY() AS ERRORSEVERITY,
ERROR_STATE() AS ERRORSTATE,
ERROR_PROCEDURE() AS ERRORPROCEDURE,
ERROR_LINE() AS ERRORLINE,
ERROR_MESSAGE() AS ERRORMESSAGE1
如果不可能,还有替代方案吗?
答案 0 :(得分:1)
是的,你可以做你想做的事情(如果第一次插入失败,插入另一个表)。但我会用TRY..CATCH阻止,,,,像....... < / p>
BEGIN TRY
BEGIN TRANSACTION
INSERT INTO TABLE1 VALUES ()
COMMIT TRANSACTION
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0
BEGIN
ROLLBACK TRANSACTION
END
INSERT INTO TABLE2 VALUES ()
ROLLBACK TRANSACTION
SELECT ERROR_NUMBER() AS ERRORNUMBER,
ERROR_SEVERITY() AS ERRORSEVERITY,
ERROR_STATE() AS ERRORSTATE,
ERROR_PROCEDURE() AS ERRORPROCEDURE,
ERROR_LINE() AS ERRORLINE,
ERROR_MESSAGE() AS ERRORMESSAGE1
END CATCH