使用Case语句更新另一个表

时间:2015-07-02 20:21:29

标签: sql-server case using

我有一张表中有一条记录。我需要根据一条记录的TYPE列更新另一个表中的8个字段。我认为Case的说法是要走的路,但是我把我的野兔拉了出来!我搜索并看到了一些接近但不完全的例子!请帮忙!这是我的代码。

UPDATE [dbo].[PART_DIMENSION]

SELECT [SELL_PACK],
    CASE q.PACK_TYPE WHEN 'SELL' then 1,
              [SELL_PACK_UOM] = Q.PACK_ASSMBY,
              [SELL_PACK_QTY] = Q.QTY,
              [SELL_PACK_LENGTH] = Q.LENGTH,
              [SELL_PACK_WIDTH] = Q.WIDTH,
              [SELL_PACK_HEIGHT] = Q.HEIGHT,
              [SELL_PACK_WEIGHT] = Q.WEIGHT,
              [SELL_PACK_INNER_QTY] = Q.INNER_QTY,
              [SELL_PACK_CAN_NEST] = Q.CAN_NEST,
              [SELL_PACK_NEED_RETAIL_PACK] = Q.NEED_RETAIL_PACK
            FROM PART_DIMENSION as a
            INNER JOIN Qubiscan as q
            on a.part_id=q.part_id

    WHEN q.PACK_TYPE='INNER' then
           SET[INNER_PACK]  = 1,
              [INNER_PACK_UOM] = Q.PACK_ASSMBY,
              [INNER_PACK_QTY] = Q.QTY,
              [INNER_PACK_LENGTH] = Q.LENGTH,
              [INNER_PACK_WIDTH] = Q.WIDTH,
              [INNER_PACK_HEIGHT] = Q.HEIGHT,
              [INNER_PACK_WEIGHT] = Q.WEIGHT
            FROM PART_DIMENSION as a
            INNER JOIN Qubiscan as q
            on a.part_id=q.part_id

    WHEN q.PACK_TYPE='MASTER' then
           SET[MASTER_PACK] = 1,
              [MASTER_PACK_UOM] = Q.PACK_ASSMBY,
              [MASTER_PACK_QTY] = Q.QTY,
              [MASTER_PACK_LENGTH] = Q.LENGTH,
              [MASTER_PACK_WIDTH] = Q.WIDTH,
              [MASTER_PACK_HEIGHT] = Q.HEIGHT,
              [MASTER_PACK_WEIGHT] = Q.WEIGHT
            FROM PART_DIMENSION as a
            INNER JOIN Qubiscan as q
            on a.part_id=q.part_id

2 个答案:

答案 0 :(得分:1)

一次完成所有事情是一个很好的目标,但有时你可以尝试一次做太多。 CASE可能不是这种情况下的最佳选择,因为做你想要的东西真的需要大约24个CASE语句......可行,但我不确定它的可读性。我建议首先把它分解成一些更简单的块...考虑这个(希望最小的拼写错误:)):

Update a
  from part_dimension a
  join qubiscan q
    on a.part_id=q.part_id
   set           a.[MASTER_PACK] = 1,
              a.[MASTER_PACK_UOM] = Q.PACK_ASSMBY,
              a.[MASTER_PACK_QTY] = Q.QTY,
              a.[MASTER_PACK_LENGTH] = Q.LENGTH,
              a.[MASTER_PACK_WIDTH] = Q.WIDTH,
              a.[MASTER_PACK_HEIGHT] = Q.HEIGHT,
              a.[MASTER_PACK_WEIGHT] = Q.WEIGHT
where q.pack_type='MASTER'

对其他两个字段列表/ pack_types重复此块。

答案 1 :(得分:0)

由于您正在查找更新语句中的--SELL UPDATE p1 SET p1.[SELL_PACK] = 1 ,p1.[SELL_PACK_UOM] = Q.PACK_ASSMBY ,p1.[SELL_PACK_QTY] = Q.QTY ,p1.[SELL_PACK_LENGTH] = Q.[LENGTH] ,p1.[SELL_PACK_WIDTH] = Q.WIDTH ,p1.[SELL_PACK_HEIGHT] = Q.HEIGHT ,p1.[SELL_PACK_WEIGHT] = Q.[WEIGHT] ,p1.[SELL_PACK_INNER_QTY] = Q.INNER_QTY ,p1.[SELL_PACK_CAN_NEST] = Q.CAN_NEST ,p1.[SELL_PACK_NEED_RETAIL_PACK] = Q.NEED_RETAIL_PACK FROM [dbo].[PART_DIMENSION] p1 INNER JOIN Qubiscan AS q ON p1.part_id = q.part_id WHERE q.pack_type = 'SELL'; --INNER UPDATE p2 SET p2.[INNER_PACK] = 1 ,p2.[INNER_PACK_UOM] = Q.PACK_ASSMBY ,p2.[INNER_PACK_QTY] = Q.QTY ,p2.[INNER_PACK_LENGTH] = Q.[LENGTH] ,p2.[INNER_PACK_WIDTH] = Q.WIDTH ,p2.[INNER_PACK_HEIGHT] = Q.HEIGHT ,p2.[INNER_PACK_WEIGHT] = Q.[WEIGHT] FROM [dbo].[PART_DIMENSION] p2 INNER JOIN Qubiscan AS q ON p2.part_id = q.part_id WHERE q.pack_type = 'INNER'; --MASTER UPDATE p3 SET p3.[MASTER_PACK] = 1 ,p3.[MASTER_PACK_UOM] = Q.PACK_ASSMBY ,p3.[MASTER_PACK_QTY] = Q.QTY ,p3.[MASTER_PACK_LENGTH] = Q.[LENGTH] ,p3.[MASTER_PACK_WIDTH] = Q.WIDTH ,p3.[MASTER_PACK_HEIGHT] = Q.HEIGHT ,p3.[MASTER_PACK_WEIGHT] = Q.[WEIGHT] FROM [dbo].[PART_DIMENSION] p3 INNER JOIN Qubiscan AS q ON p3.part_id = q.part_id WHERE q.pack_type = 'MASTER'; 版本的连接,因此您可以将更新拆分为三个不同的语句,并执行以下操作:

    static string removeTagsInBrackets(string input)
    {            
        StringBuilder sb = new StringBuilder(input.Length);
        bool insideBrackets = false;
        bool insideTag = false; char c;
        int indexOfLast = input.LastIndexOf(']');
        for (int i = 0; i < input.Length; i++)
        {
            c = input[i];
            if (c == '[') { insideBrackets = true; sb.Append(c); continue; }
            if (i == indexOfLast) { insideBrackets = false; sb.Append(c); continue; }
            if (c == '<' || c == '>') { insideTag = !insideTag; }
            if (insideBrackets) if (insideTag || (!insideTag && c == '>')) continue;
            sb.Append(c);
        }
        return sb.ToString();
    }

希望这有帮助!