SQLServer存储过程事务用法
IF object_id('InsertAntennaProcedure') IS NOT NULL DROP PROCEDURE InsertAntennaProcedure GO CREATE PROCEDURE InsertAntennaProcedure @INST_Model VARCHAR(36), @INST_Manufacturer VARCHAR(36), @INST_CalibrationDate datetime, @INST_Pictures image AS BEGIN BEGIN TRANSACTION BEGIN TRY DECLARE @currentAntennaID INT; INSERT INTO tb_Antenna ( INST_Model, INST_Manufacturer, INST_CalibrationDate, INST_Pictures ) VALUES ( @INST_Model, @INST_Manufacturer, @INST_CalibrationDate, @INST_Pictures ) SELECT @currentAntennaID=IDENT_CURRENT('tb_Antenna') END TRY BEGIN CATCH IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION; DECLARE @ErrorMessage NVARCHAR(4000); DECLARE @ErrorSeverity INT; DECLARE @ErrorState INT; SELECT @ErrorMessage = ERROR_MESSAGE(), @ErrorSeverity = ERROR_SEVERITY(), @ErrorState = ERROR_STATE(); -- Use RAISERROR inside the CATCH block to return error -- information about the original error that caused -- execution to jump to the CATCH block. RAISERROR (@ErrorMessage, -- Message text. @ErrorSeverity, -- Severity. @ErrorState -- State. ); END CATCH IF @@TRANCOUNT > 0 COMMIT TRANSACTION; END GO EXEC InsertAntennaProcedure '123', '123', '2014-01-01',NULL;