我有这样的存储过程:
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_table
匹配TransactID
更新表格@carid
。
我怎么能这样做?
答案 0 :(得分: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
答案 1 :(得分: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
答案 2 :(得分: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次更新,因此要么成功要么都不成功。