2017-07-28 37 views
0

我是新來的SQL,我想用ROLLBACK TRANSACTION在SQL所以我這樣做SQL事務

--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 

我叫我的窗口,這些存儲過程中形成的應用C#。 TIA

+0

你的問題是? – Xedni

+0

是對還是不是?因爲它不工作在我的Windows窗體應用程序 – Faiq

+2

閱讀如何提出問題https://stackoverflow.com/help/how-to-ask和事務https://docs.microsoft.com/en-us/sql/t -sql/language-elements/transactions-transact-sql – Eric

回答

0
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 
+0

你能解釋一下IF條件嗎? TIA – Faiq

+0

如果我得到的錯誤不等於零然後我回滾交易,它總是爲我工作 –