如何将变量传递给存储过程以更新另一个数据库中的行?

How do I pass a variable to a stored procedure to update a row in another database?

我需要从一个数据库中为客户获取一个值,并用该值更新另一个数据库。

下面的过程有效,但我需要让它通过 table2 并使用匹配的 CustomerID 更新 table1 中的每个客户。我讨厌使用循环这个词,但正如我所说,我对此很陌生并且迷路了。我看过视频,并尝试搜索但没有运气。有人可以指点我一个教程或告诉我我是否正在尝试做一些我不应该做的事情吗?

CREATE PROCEDURE dbo.bhshSample
as 
BEGIN;
    update table1 set
        ptall = (
            SELECT TOP (1) nsma1_ans
            from table2
            where nsma1_code = 'ptall'
            order by nsma1_tm
        )
    where CustomerID = '4'
End;

在 php 中,我会循环并执行 select 与 table 2

不同的 CustomerID

然后使用我设置的变量进行更新,但我似乎无法使用存储过程解决它。

使用相关子查询,像这样:

update table1 set
    ptall = (
        SELECT TOP (1) nsma1_ans
        from table2
        where nsma1_code = 'ptall'
        and CustomerID = table1.CustomerID
        order by nsma1_tm
    )