使用多个 SQL 语句调用存储过程,以便执行两个 SQL 语句或 none 个语句

Call the stored procedure with multiple SQL statements such that either both of the SQL statements should executed or none of them

CREATE proc [dbo].[spCreation]
(
    @ProgramId nvarchar(20), @S_26 int = 0, @S_28 int = 0, @S_30 int = 0,
    @S_32 int = 0, @S_34 int = 0, @S_36 int = 0, @S_38 int = 0,
    @S_40 int = 0, @S_42 int = 0, @S_44 int = 0, @S_46 int = 0, @S_48 int = 0
)
AS
BEGIN
    insert into tblCreation (ProgramId, S_26, S_28, S_30, S_32, S_34, S_36, S_38, S_40, S_42, S_44, S_46, S_48, Quantity)
    values(@ProgramId, @S_26, @S_28, @S_30, @S_32, @S_34, @S_36, @S_38, @S_40, @S_42, @S_44, @S_46, @S_48,
@S_26+@S_28+@S_30+@S_32+@S_34+@S_36+@S_38+@S_40+@S_42+@S_44+@S_46+@S_48)

    update tblProgramme 
    set ProgramStatus = 'In Process' 
    where ProgramId = @ProgramId
END

这是我的存储过程。我将在 ado.net 中调用该存储过程。 我的问题是 如果第二个 sql 语句的 table 名称或列名称将在 future.will 中通过异常更改或仅执行第一条语句,将会发生什么情况。

这些列名称看起来不太友好,希望它们对您的项目有特定意义。

你需要的是一笔交易,我很快就整理好了:

CREATE proc [dbo].[spCreation]
(
@ProgramId nvarchar(20), @S_26 int = 0, @S_28 int = 0, @S_30 int = 0,
@S_32 int = 0, @S_34 int = 0, @S_36 int = 0, @S_38 int = 0,
@S_40 int = 0, @S_42 int = 0, @S_44 int = 0, @S_46 int = 0, @S_48 int = 0
)
AS
BEGIN

BEGIN TRAN

insert into tblCreation (ProgramId, S_26, S_28, S_30, S_32, S_34, S_36, S_38, S_40, S_42, S_44, S_46, S_48, Quantity) values(@ProgramId, @S_26, @S_28, @S_30, @S_32, @S_34, @S_36, @S_38, @S_40, @S_42, @S_44, @S_46, @S_48, @S_26+@S_28+@S_30+@S_32+@S_34+@S_36+@S_38+@S_40+@S_42+@S_44+@S_46+@S_48)

update tblProgramme 
set ProgramStatus = 'In Process' 
where ProgramId = @ProgramId

IF @@ERROR <> 0 BEGIN 
    ROLLBACK TRAN 
    return 0 
END 

COMMIT TRAN
END 
GO

进一步阅读:

http://www.sqlteam.com/article/introduction-to-transactions

https://msdn.microsoft.com/en-us/library/ms188929.aspx