我想从名为Sorels的表中更新名为Sorels_ext的表。它们之间的链接是Sorels_ext的fkey_id等于Sorels表的identity_column。这是我在Merge语句中的第一次尝试,我正在尝试学习语法。
MERGE Sorels_ext AS SORe
USING (select SOR.identity_column, CASE
WHEN left(SOR.FPARTNO, 2) = 'BL' THEN 'Blue'
WHEN left(SOR.FPARTNO, 2) = 'RD' THEN 'Red'
ELSE 'White'
END from Sorels AS SOR)
ON (SORe.fkey_id = SOR.identity_column)
WHEN MATCHED THEN
UPDATE SET SORe.fkey_id = SOR.identity_column, SORe.Color = select SOR.identity_column, CASE
WHEN left(SOR.FPARTNO, 2) = 'BL' THEN 'Blue'
WHEN left(SOR.FPARTNO, 2) = 'RD' THEN 'Red'
ELSE 'White'
END
WHEN NOT MATCHED THEN
INSERT (SORe.fkey_id, SORe.Color) VALUES (SOR.identity_column, SORe.Color = select SOR.identity_column, CASE
WHEN left(SOR.FPARTNO, 2) = 'BL' THEN 'Blue'
WHEN left(SOR.FPARTNO, 2) = 'RD' THEN 'Red'
ELSE 'White'
END);
当我运行它时,我收到以下错误:
错误10/22/2009 1:38:51 PM 0:00:00.000 SQL Server数据库错误:关键字“ON”附近的语法不正确。 46 0
***添加信息******
建议第一次修复后,代码如下:
MERGE Sorels_ext AS SORe
USING (select SOR.identity_column, CASE
WHEN left(SOR.FPARTNO, 2) = 'BL' THEN 'Blue'
WHEN left(SOR.FPARTNO, 2) = 'RD' THEN 'Red'
ELSE 'White'
END from Sorels) AS SOR
ON (SORe.fkey_id = SOR.identity_column)
WHEN MATCHED THEN
UPDATE SET SORe.fkey_id = SOR.identity_column, SORe.Color = CASE
WHEN left(SOR.FPARTNO, 2) = 'BL' THEN 'Blue'
WHEN left(SOR.FPARTNO, 2) = 'RD' THEN 'Red'
ELSE 'White'
END
WHEN NOT MATCHED THEN
INSERT (SORe.fkey_id, SORe.Color) VALUES (SOR.identity_column, CASE
WHEN left(SOR.FPARTNO, 2) = 'BL' THEN 'Blue'
WHEN left(SOR.FPARTNO, 2) = 'RD' THEN 'Red'
ELSE 'White'
END);
现在我收到以下错误:
错误10/22/2009 2:03:29 PM 0:00:00.000 SQL Server数据库错误:MERGE语句中使用的插入列列表不能包含多部分标识符。请改用单个部件标识符。 55 0
*******添加更多信息****** 根据建议调整后,我有以下内容:
MERGE Sorels_ext AS SORe
USING (select SOR1.identity_column, CASE
WHEN left(SOR1.FPARTNO, 2) = 'BL' THEN 'Blue'
WHEN left(SOR1.FPARTNO, 2) = 'RD' THEN 'Red'
ELSE 'White'
END as colors from Sorels as SOR1 ) as SOR
ON (SORe.fkey_id = SOR.identity_column)
WHEN MATCHED THEN
UPDATE SET SORe.fkey_id = SOR.identity_column, SORe.Color = CASE
WHEN left(SOR.FPARTNO, 2) = 'BL' THEN 'Blue'
WHEN left(SOR.FPARTNO, 2) = 'RD' THEN 'Red'
ELSE 'White'
END
WHEN NOT MATCHED THEN
INSERT (fkey_id, Color) VALUES (SOR.identity_column, CASE
WHEN left(SOR.FPARTNO, 2) = 'BL' THEN 'Blue'
WHEN left(SOR.FPARTNO, 2) = 'RD' THEN 'Red'
ELSE 'White'
END);
我收到错误:
错误10/22/2009 2:46:51 PM 0:00:00.000 SQL Server数据库错误:列名“FPARTNO”无效。 56 0
我做错了什么?
****我知道了! *****
MERGE Sorels_ext AS SORe
USING (select SOR.identity_column, CASE
WHEN left(SOR.FPARTNO, 2) = 'BL' THEN 'Blue'
WHEN left(SOR.FPARTNO, 2) = 'RD' THEN 'Red'
ELSE 'White'
END as colors from Sorels as SOR) SOR1
ON (SORe.fkey_id = SOR1.identity_column)
WHEN MATCHED THEN
UPDATE SET SORe.fkey_id = SOR1.identity_column, SORe.Color = SOR1.colors
WHEN NOT MATCHED THEN
INSERT (fkey_id, Color) VALUES (SOR1.identity_column, SOR1.colors);
答案 0 :(得分:5)
INSERT (SORe.fkey_id, SORe.Color)
应为:
INSERT (fkey_id, Color)
插入列表中的列只能引用目标表。解析器不希望在那里看到表别名,并且不知道如何解决它。
如果它看到“column1”,它知道它属于目标表。它看到“table1.column1”,它不知道“table1”是什么意思,因为“table1”作为一个标记超出了范围。
答案 1 :(得分:4)
我认为您必须为您的源数据添加别名:
USING (select SOR.identity_column,
CASE WHEN left(SOR.FPARTNO, 2) = 'BL'
THEN 'Blue'
WHEN left(SOR.FPARTNO, 2) = 'RD'
THEN 'Red'
ELSE 'White'
END from Sorels AS SOR) **AS SOR** ON (SORe.fkey_id = SOR.identity_column)
答案 2 :(得分:0)
尝试取出“AS SOR”中的“AS”