2013-06-22 43 views
3

我已存儲的過程是這樣的:存儲過程更新兩個表不同的ID

alter procedure [dbo].[delivary] @dedate nvarchar(100), 
           @carid nvarchar(100), 
           @transid integer as 
begin 
    select t.transactID 
     from Transaction_tbl t 
    where t.TBarcode = @carid 
    update Transaction_tbl 
     set DelDate = '' + @dedate + '', Status=5 
    where TBarcode = @carid 
    update KHanger_tbl 
     set Delivered=1 
    where [email protected] 
end 

我能更新我的事務表。 我也想更新表KHanger_tableTransactID匹配@carid

我該怎麼做?

回答

0

應該

alter procedure [dbo].[delivary] 
    (@dedate nvarchar(100), 
    @carid nvarchar(100)) 
    AS 
    begin 
     DECLARE @transactID int; 
     SET @transactID = (select t.transactID 
      from Transaction_tbl t 
     where t.TBarcode = @carid); 

     update Transaction_tbl 
      set DelDate = '' + @dedate + '', Status=5 
     where TBarcode = @carid 

     update KHanger_tbl 
      set Delivered=1 
     where [email protected] 
    end 
1

有2種方式,你可以做到這一點:

首先檢索和存儲在一個變量您transactID:

alter procedure [dbo].[delivary] @dedate nvarchar(100), 
           @carid nvarchar(100)as 
begin 
    declare @transid int 
    select @transid = t.transactID 
     from Transaction_tbl t 
    where t.TBarcode = @carid 

    update Transaction_tbl 
     set DelDate = '' + @dedate + '', Status=5 
    where TBarcode = @carid 

    update KHanger_tbl 
     set Delivered=1 
    where [email protected] 
end 

和你有關係更新:

alter procedure [dbo].[delivary] @dedate nvarchar(100), 
           @carid nvarchar(100) as 
begin 
    update Transaction_tbl 
     set DelDate = '' + @dedate + '', Status=5 
    where TBarcode = @carid 

    update KHt 
     set KHt.Delivered=1 
    from KHanger_tbl as KHt 
     inner join Transaction_tbl t 
     on KHt.transactionid = t.transactID 
    where t.TBarcode = @carid 
end 
+0

這工作正常,,先生,,,謝謝 – user2510547

0

這裏的另一個(短)的方式來做到這一點:

alter procedure [dbo].[delivary] 
(@dedate nvarchar(100), 
@carid nvarchar(100)) 
AS 
begin 
    DECLARE @transactID int; 

    update Transaction_tbl 
     set DelDate = @dedate, Status=5, @transactID = transactID 
    where TBarcode = @carid 

    update KHanger_tbl 
     set Delivered=1 
    where [email protected] 
end 

而且,你可能想要做在一個事務中的2次更新,所以兩個都成功,要麼根本就沒有。