1

我是存储过程和事务的新手。

我编写了一个将数据插入两个表的存储过程。这是SP:

Begin

SET NOCOUNT ON 

Declare @Parcel_Id int

INSERT INTO 
        Parcel([Code],[Title],
               [Is_Parcel_Equal_To_Property],
               [Property_Id],[Property_Type_Id],
               [Plot_No],[Phone],[Fax],[Email],
               [Administrator_Name],
               [Watchman_Name],
               [Remark],
               [List_in_Retail_Leasing_Module],
               [Landlord_Id],
               [Country_Id],[State_Id],[Community_Id],[MainRoad_Id],
               [Address],
               [Is_Active],
               [Added_On_Date],[Added_By]) 
        VALUES(@Code,@Title,
               @Is_Parcel_Equal_To_Property,
               @Property_Id,@Property_Type_Id,
               @Plot_No,@Phone,@Fax,@Email,
               @Administrator_Name,
               @Watchman_Name,
               @Remark,
               @List_in_Retail_Leasing_Module,
               @Landlord_Id,
               @Country_Id,@State_Id,@Community_Id,@MainRoad_Id,
               @Address,
               @Is_Active,
               @Added_On_Date,@Added_By) 

Set @Parcel_Id = SCOPE_IDENTITY()

INSERT INTO Parcel_Spacifications
        ([Parcel_Id],[Apartments],[Villas],[Retails],[Offices],[Labour_Camp],
         [Warehouses],[Total_Car_Parks],[Floors],[Year_Of_Construction],
         [Built_Up_Area],[Plot_Area],[No_Of_Basements],[Is_Special_Project])
        VALUES (@Parcel_Id,@Apartments,@Villas,@Retails,@Offices,@Labour_Camp,
                @Warehouses,@Total_Car_Parks,@Floors,@Year_Of_Construction,
                @Built_Up_Area,@Plot_Area,@No_Of_Basements,@Is_Special_Project)               
End
RETURN @Parcel_Id

现在我想使用交易,怎么做?

4

1 回答 1

1

BEGIN TRAN你用和COMMIT像这样包装你的代码:

BEGIN

SET NOCOUNT ON

Declare @Parcel_ID int

BEGIN TRAN

...

COMMIT

END

RETURN @Parcel_ID

您应该在此处阅读文档中的备注。他们解释了交易是如何运作的。

于 2012-11-29T15:33:54.580 回答