'附近的语法不正确。'。在更新变量表时

时间:2016-09-07 14:07:20

标签: sql sql-server sql-server-2008 syntax-error

更新表变量时遇到问题。我已经宣布了它,但是当我运行整个查询时,我收到了这个错误:

  

'附近的语法不正确。'。

我似乎无法解决这个问题......

UPDATE @List 
SET nrID = CASE 
              WHEN nrID IS NULL 
                 THEN t1.nrID 
                 ELSE nrID 
           END 
FROM @List 
INNER JOIN table1 t1 ON @List.PNr = t1.PNr
INNER JOIN table2 t2 ON t1.n nrPNr = t2.nrPNr
WHERE t1.PNr = t2.PNr AND t2.Isactive = 0

3 个答案:

答案 0 :(得分:1)

UPDATE List 
SET List.nrID = t1.nrID
FROM @List AS List
INNER JOIN table1 t1 ON List.nrID = t1.nrID
INNER JOIN table2 t2 ON t1.nrID = t2.nrID
WHERE t1.nrID = t2.nrID AND t2.Isactive = 0

如评论中所述,内部联接将删除案例中的任何空值

答案 1 :(得分:0)

感谢您的帮助,将我声明的@table更改为创建@temptable,它消除了我错误的语法错误。

至于我使用的查询(示例);

UPDATE Person
   SET Car = ISNULL(@Car, Car),
       HairColour = ISNULL(@HairColour, HairColour),

答案 2 :(得分:0)

试试这个,

UPDATE L
SET L.nrID = ISNULL(t1.nrID, t2.nrID)
FROM @List L
INNER JOIN table1 t1 ON L.PNr = t1.PNr
INNER JOIN table2 t2 ON t1.nrPNr = t2.nrPNr
WHERE t1.PNr = t2.PNr
    AND t2.Isactive = 0