3

我有这样的存储过程:

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 transactid=@transid
end

我能够更新我的交易表。我还想KHanger_tableTransactID匹配的@carid.

我怎么能这样做?

4

3 回答 3

1

有两种方法可以做到:

首先检索您的 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 transactid=@transid
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
于 2013-06-22T14:21:51.440 回答
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 transactid=@transactID
    end
于 2013-06-22T14:21:49.970 回答
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 transactid=@transactID
end

此外,您可能希望在事务中进行 2 次更新,因此要么都成功,要么根本不成功。

于 2013-06-23T00:32:16.860 回答