SQL Transaction坚持打开

时间:2015-06-16 15:26:20

标签: sql sql-server-2012

你好我正在创建一个存储的PROC,所以我进入了一个新的查询,在我创建存储的PROC之前输入以下SQL进行测试,现在当我输入select * From InvoicesT时;它只是继续尝试而不返回任何东西。我相信交易仍然开放请告诉我离开。

    BEGIN TRAN

        INSERT INTO InvoicesT(CompanyID,ContactsID, DueDate, Paid, Estimate, Billed) 
                    VALUES(
                           (SELECT c.[CompanyID] FROM CompanyT AS C WHERE c.ComapnyName = 'Jaila Files'),
                           (SELECT co.ID FROM ContactT as co WHERE co.LastName +', ' +co.FirstName = 'Tays, James'),
                           GETDATE()+7,
                           0,
                           0,
                           0
                           );
        go
        DECLARE @InvoiceNum int 
        @InvoiceNum = SELECT distinct SCOPE_IDENTITY() FROM InvoicesT

        Insert INTO InvoiceItems ([InvoiceNumber],[Amount],[Description],[EmployeeID],[Hours],[Product])
           VALUES (
                    @InvoiceNum, --Invoice Number
                    0.01, -- amount
                    'This is a Test', --Description
                    (SELECT e.EmpolyeeID FROM EmployeesT AS e WHERE e.FirstName = 'James' AND e.LastName = 'Tays'), -- EmployeeID
                    1, -- Hours
                    (SELECT p.ID FROM ProductsT as p WHERE p.ProductName = 'Excel')
                  )     
 COMMIT TRAN

DECLARE @InvoiceNum

出错

1 个答案:

答案 0 :(得分:2)

你可以执行

ROLLBACK TRAN

来自同一查询窗口。

错误并未中止交易,因为您在GO之前在单独的批次中启动了交易。

如果您没有相同的连接,则必须查找并终止该交易。请参阅close/kill transaction