我想从一个名为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);
我相信您必须像这样对源数据进行别名:
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)