SQL Update - 一个SQL语句中的多行和多个Critieria

时间:2015-07-07 14:57:00

标签: sql case case-when

我正在尝试根据两个不同的标准更新两个字段,我无法弄清楚如何在单个SQL语句中完成此操作。

这可能没有循环吗?

以下是我想要完成的单行示例:

C#

这是我在网上发现的声明,即我没有成功修改这项工作。

Update Leadtimes Set Leadtime = 10 Where supplier = 123 and location = 1;
Update Leadtimes Set Transit_Leadtime = 5 Where supplier = 123 and location = 1;
Update Leadtimes Set Leadtime = 12 Where supplier = 123 and location = 2;
Update Leadtimes Set Transit_Leadtime = 6 Where supplier = 123 and location = 2;
Update Leadtimes Set Leadtime = 5 Where supplier = 223 and location = 1;
Update Leadtimes Set Transit_Leadtime = 3 Where supplier = 223 and location = 1;
Update Leadtimes Set Leadtime = 7 Where supplier = 223 and location = 2;
Update Leadtimes Set Transit_Leadtime = 2 Where supplier = 223 and location = 2;

2 个答案:

答案 0 :(得分:1)

Update Leadtimes 
    Set 
        Leadtime = CASE CAST(supplier AS VARCHAR(MAX)) + CAST(location AS VARCHAR(MAX))
            WHEN '1231' THEN 10
            WHEN '1232' THEN 12
            WHEN '2231' THEN 5
            WHEN '2232' THEN 7
        END,
        Transit_Leadtime = CASE CAST(supplier AS VARCHAR(MAX)) + CAST(location AS VARCHAR(MAX))
            WHEN '1231' THEN 5
            WHEN '1232' THEN 6
            WHEN '2231' THEN 3
            WHEN '2232' THEN 2
        END 
Where supplier in (123, 223) and location in (1, 2);

结果如下:

enter image description here

答案 1 :(得分:0)

Update Leadtimes Set Leadtime = 10 Where supplier = 123 and location = 1;
Update Leadtimes Set Transit_Leadtime = 5 Where supplier = 123 and location = 1;

可以写成:

UPDATE Leadtimes SET Leadtime = 10, Transit_Leadtime = 5 WHERE supplier = 123 AND location = 1;

我会准备一份表格声明:

UPDATE Leadtimes SET Leadtime = ?, Transit_Leadtime = ? WHERE supplier = ? AND location = ?;

然后为我需要的所有数据四边形执行它。