我有一个表更新两列,如果第二列更新大于或等于4,则将另一列设置为0
尝试过案例陈述......但仍然无法正常工作请帮助!
update [dbo].[QueuedSms_TEST]
set issent = 0,pendingstatusid = pendingstatusid + 1
where sendresponse is null
while (select pendingstatusid from [dbo].[QueuedSms_TEST]) > 3
update [dbo].[QueuedSms_TEST]
set issent = -1
答案 0 :(得分:0)
首先创建一个时态查询来获取row_number,然后使用所需的规则更新它。
<强> SQL Demo 强>
WITH cte as (
SELECT *,
ROW_NUMBER() OVER (ORDER BY pendingstatusid) as rn
FROM QueuedSms_TEST
)
UPDATE cte
SET pendingstatusid = rn,
issent = CASE WHEN rn > 3
THEN -1
ELSE 0
END;