2

我如何更改以下过程,如果由于 PrimaryKey 或某些原因未成功执行插入语句,则也不得执行 Delete 语句,而且它应该生成一条我自己编写的错误消息。

CREATE PROCEDURE [dbo].[ReAdmissionInsert]
@GRNo varchar(4),
@ClassId numeric(2),
@FacultyId numeric(1),
@Section varchar(1),
@SessionId numeric(1)
AS
begin
insert into ReAdmissionDtl(GRNo,ClassId,FacultyId,Section,SessionId) values(@GRNo,@ClassId,@FacultyId,@Section,@SessionId)
delete from Discharge where GRNo = @GRNo
end
4

3 回答 3

2

您使用BEGIN TRAN&创建一个事务,如果您的or失败COMMIT将回滚:INSERTDELETE

CREATE PROCEDURE [dbo].[Readmissioninsert] @GRNo      VARCHAR(4),
                                           @ClassId   NUMERIC(2),
                                           @FacultyId NUMERIC(1),
                                           @Section   VARCHAR(1),
                                           @SessionId NUMERIC(1)
AS
  BEGIN
      BEGIN TRAN  --<= Starting point of transaction

      INSERT INTO readmissiondtl
                  (grno,
                   classid,
                   facultyid,
                   section,
                   sessionid)
      VALUES     (@GRNo,
                  @ClassId,
                  @FacultyId,
                  @Section,
                  @SessionId)

      DELETE FROM discharge
      WHERE  grno = @GRNo

      COMMIT --<= End point of transaction
  END 

文档

您可以将 aTRY CATCH用于错误消息:

于 2012-10-22T17:27:18.917 回答
1

使用事务和 try catch 块。在 catch 块中提出错误,如下所示:

CREATE PROCEDURE [dbo].[ReAdmissionInsert]
@GRNo varchar(4),
@ClassId numeric(2),
@FacultyId numeric(1),
@Section varchar(1),
@SessionId numeric(1)
AS
begin
Begin transaction
Begin try
    insert into ReAdmissionDtl(GRNo,ClassId,FacultyId,Section,SessionId)
       values(@GRNo,@ClassId,@FacultyId,@Section,@SessionId)
    delete from Discharge where GRNo = @GRNo
    Commit transaction
End try
Begin catch
    Rollback
    Raiserror(999999,'my message',16,1)
End catch
end
于 2012-10-22T17:40:28.060 回答
1

使用事务编写 SQL Server 存储过程的最佳实践 -

  1. 封闭TRY..CATCH
  2. 错误检查@@TRANCOUNTROLLBACK交易
  3. RAISE实际ERROR报警调用程序

样本 -

CREATE PROCEDURE [dbo].[ReAdmissionInsert]
@GRNo varchar(4),
@ClassId numeric(2),
@FacultyId numeric(1),
@Section varchar(1),
@SessionId numeric(1)
AS
BEGIN
SET NOCOUNT ON
BEGIN TRY   
    BEGIN TRAN

    insert into ReAdmissionDtl(GRNo,ClassId,FacultyId,Section,SessionId)
       values(@GRNo,@ClassId,@FacultyId,@Section,@SessionId)

    delete from Discharge where GRNo = @GRNo

    COMMIT TRAN     
END TRY    
BEGIN CATCH

    IF @@TRANCOUNT > 0
        ROLLBACK TRAN

    /*ERROR OCCURED*/  
    DECLARE @ERROR_MESSAGE NVARCHAR(4000);--MESSAGE TEXT           
    DECLARE @ERROR_SEVERITY INT;--SEVERITY           
    DECLARE @ERROR_STATE INT;--STATE        

    SELECT @ERROR_MESSAGE = ERROR_MESSAGE(),        
        @ERROR_SEVERITY = ERROR_SEVERITY(),        
        @ERROR_STATE = ERROR_STATE() 


    /*RETURN ERROR INFORMATION ABOUT THE ORIGINAL ERROR THAT CAUSED
      EXECUTION TO JUMP TO THE CATCH BLOCK.*/           
    RAISERROR (@ERROR_MESSAGE, @ERROR_SEVERITY, @ERROR_STATE)

END CATCH   
END

请注意,@@TRANCOUNT已检查以验证是否有任何打开的事务和ERROR消息被保留并引发,因此程序将接收SqlException.

于 2012-10-22T19:02:42.230 回答