运行 存储过程中同一 table 的多个更新语句使用来自 table 值参数的值
Run multiple update statements on same table in stored procedure using values from table vlued parameter
我想使用 table 赋值参数的值更新 table 中的一列,然后我想根据该列的值更新另一列(相同的行)更新第一个声明。这是代码:
@reports as WhatsTableType ReadOnly
BEGIN
update dbo.tblappointments Set dbo.tblappointments.D_report = r.D_report
from dbo.tblappointments as A inner join @reports as r on A.appointmentID = r.appointmentID;
update dbo.tblappointments Set dbo.tblappointments.WantSMS = 0
from dbo.tblappointments as A inner join @reports as r on A.appointmentID = r.appointmentID
where A.D_report = 'Read' or A.D_report = 'DEVICE';
END
table 参数包含两列(appointmentID,D_report)。我想我可以通过 IIF 使用单个更新语句,但我不确定执行此操作的最佳方法。
谢谢。
如果 D_Report
是其他东西,或者 NULL,或者它是否应该取决于 table 中的现有值,则取决于您希望 WantSMS
变成什么,您可以这样做这在一个声明中如下:
UPDATE A Set
A.D_report = r.D_report,
A.WantSMS = CASE
-- need to check R for the _new_ value:
WHEN r.D_Report IN ('Read', 'DEVICE') THEN 0
ELSE A.WantSMS END
FROM dbo.tblappointments as A
INNER JOIN @reports as r
ON A.appointmentID = r.appointmentID;
IIF
很花哨 CASE
但我发现 CASE
更灵活。 YMMV.
我想使用 table 赋值参数的值更新 table 中的一列,然后我想根据该列的值更新另一列(相同的行)更新第一个声明。这是代码:
@reports as WhatsTableType ReadOnly
BEGIN
update dbo.tblappointments Set dbo.tblappointments.D_report = r.D_report
from dbo.tblappointments as A inner join @reports as r on A.appointmentID = r.appointmentID;
update dbo.tblappointments Set dbo.tblappointments.WantSMS = 0
from dbo.tblappointments as A inner join @reports as r on A.appointmentID = r.appointmentID
where A.D_report = 'Read' or A.D_report = 'DEVICE';
END
table 参数包含两列(appointmentID,D_report)。我想我可以通过 IIF 使用单个更新语句,但我不确定执行此操作的最佳方法。
谢谢。
如果 D_Report
是其他东西,或者 NULL,或者它是否应该取决于 table 中的现有值,则取决于您希望 WantSMS
变成什么,您可以这样做这在一个声明中如下:
UPDATE A Set
A.D_report = r.D_report,
A.WantSMS = CASE
-- need to check R for the _new_ value:
WHEN r.D_Report IN ('Read', 'DEVICE') THEN 0
ELSE A.WantSMS END
FROM dbo.tblappointments as A
INNER JOIN @reports as r
ON A.appointmentID = r.appointmentID;
IIF
很花哨 CASE
但我发现 CASE
更灵活。 YMMV.