sql - 更新具有不同 id 的两个表的存储过程

标签 sql sql-server

我有这样的存储过程:

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

我可以更新我的交易表。 我还想用匹配 @caridTransactID 更新表 KHanger_table

我该怎么做?

最佳答案

有两种方法可以做到:

首先检索您的 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

关于sql - 更新具有不同 id 的两个表的存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17251510/

相关文章:

php - sql语句放入数组导致重复

java - 使用容器进行 SQL 字符串查询

mysql:查找具有多个标签和相同 id 的行

sql - 如何在 SQL Server 中的 case 语句内执行 SUM()

sql - 如何查找记录的行号?

php - 从其他表中选择具有条件规则(计数)的数据

sql - 设计支持多语言的数据库

sql-server - 为什么我的 SQL Server 删除查询计划中的排序有问题?

sql - 查找任何属于日期列表的日期

sql - 求职面试测试