sql – 使用select语句合并插入

前端之家收集整理的这篇文章主要介绍了sql – 使用select语句合并插入前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这对我有用
MERGE Table1 AS tgt
 USING (SELECT TOP 1 * FROM Table2,(SELECT itmid FROM Table3 WHERE id=@id) as a
        WHERE id = @id) AS src
ON (tgt.id = src.id)
            WHEN MATCHED THEN UPDATE SET qty = qty + @qty
            WHEN NOT MATCHED THEN
                INSERT itmid
                VALUES itmid;

但是当我这样改变它不工作,显示最近选择附近的错误

MERGE Table1 AS tgt
 USING (SELECT TOP 1 * FROM Table2
        WHERE id = @id) AS src
ON (tgt.id = src.id)
            WHEN MATCHED THEN UPDATE SET qty = qty + @qty
            WHEN NOT MATCHED THEN
                INSERT itmid
                SELECT itmid FROM Table3 WHERE id=@id;

解决方法

根据 MSDN docs
<merge_not_matched>::=
{
    INSERT [ ( column_list ) ] 
        { VALUES ( values_list )
        | DEFAULT VALUES }
}

来自SELECT的INSERT语法如下所示:

WHEN NOT MATCHED THEN
            INSERT itmid SELECT itmid FROM Table3 WHERE id=@id;

不允许!

我会尝试解决你的问题做另一个与表3的合并.

原文链接:https://www.f2er.com/mssql/75820.html

猜你在找的MsSQL相关文章