简体   繁体   中英

Sql Transaction

I am new to sql and i wanted to use ROLLBACK TRANSACTION in sql so i did this

--to insert into instructor table--

create procedure [dbo].[insertInstructor] (@f_name varchar(50), @l_name varchar(50), @contact nvarchar(50))
AS
BEGIN
    begin transaction 
    save transaction insert_instructor
    insert into instructor(f_name,l_name,contact)
    values(@f_name,@l_name,@contact)
END


--to rollback the transaction i did this
create procedure [dbo].[delete_instructor]
 AS
 BEGIN
    begin transaction insert_instructor
    rollback transaction insert_instructor

 END

I called these stored procedure in my windows form application C#. TIA

CREATE PROCEDURE [dbo].[insertInstructor] (@f_name VARCHAR(50), @l_name VARCHAR(50), @contact NVARCHAR(50))
AS
BEGIN

BEGIN TRANSACTION
DECLARE @flg_error  INT

    SET @flg_error  = 0 

    INSERT INTO instructor(f_name,l_name,contact)
    VALUES(@f_name,@l_name,@contact)

    SET @flg_error = @@ERROR

        IF @flg_error <> 0 BEGIN
            RAISERROR ('1', 16, 1)
            ROLLBACK TRANSACTION

        END

    COMMIT TRANSACTION

END 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM