选择多个时更新一个值

时间:2018-08-28 17:25:21

标签: sql sql-server sql-update

与查询该问题时相比,我的SQL查询不那么直接,因此我希望在查询中获得一些帮助。

以下查询选择了三个特定值,我需要一个未被选择的值进行更新。

SET ANSI_NULLS, ANSI_WARNINGS, ARITHABORT ON;
SELECT * FROM (
  SELECT EXP.EmpID,
  -1 * EXP.TotalAmount AS Amount,
  EXP.Name
  FROM TBL EXP
  WHERE EXP.ID IN (1, 2, 3) --this is dynamically created
  AND EXP.Status = 'APPROVED'
) AS ResultTable

从该查询中,我需要将{.1}的EXP.Status值设置为'COMPLETE'。

我该怎么做?

3 个答案:

答案 0 :(得分:1)

您不只是使用MaxNumDates吗?

UPDATE

答案 1 :(得分:1)

您也许也可以尝试以下方法:

UPDATE EXP
SET EXP.Status = 'COMPLETE'
FROM TBL EXP
OUTPUT INSERTED.EmpID,-1 * INSERTED.TotalAmount AS Amount, INSERTED.Name
WHERE EXP.ID IN (1, 2, 3) AND EXP.Status = 'APPROVED';

答案 2 :(得分:0)

我意识到我不需要将每个语句合并为一个。因此,我要做的只是在我的UPDATE语句之后添加一个SELECT语句。

SET ANSI_NULLS, ANSI_WARNINGS, ARITHABORT ON;
SELECT * FROM (
  SELECT EXP.EmpID,
  -1 * EXP.TotalAmount AS Amount,
  EXP.Name
  FROM TBL EXP
  WHERE EXP.ID IN (1, 2, 3) --this is dynamically created
  AND EXP.Status = 'APPROVED'
) AS ResultTable

UPDATE TBL
SET Status = 'COMPLETE'
WHERE ID IN (1, 2, 3) --same dynamically created variable as above SELECT statement