我该如何在不存在的地方插入?

编程入门 行业动态 更新时间:2024-10-25 00:23:47
本文介绍了我该如何在不存在的地方插入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想将插入查询与不存在的地方"组合在一起,以免违反PK约束.但是,如下所示的语法给了我Incorrect syntax near the keyword 'WHERE'错误-

I'd like to combine an insert query with a "where not exists" so as not to violate PK constraints. However, syntax such as the following gives me an Incorrect syntax near the keyword 'WHERE' error -

INSERT INTO myTable(columns...) VALUES(values...) WHERE NOT EXISTS (SELECT * FROM myTable WHERE pk_part1 = value1, AND pk_part2 = value2)

我该怎么做?

(通常,您可以将插入内容与where子句结合使用吗?)

(In general, can you combine an insert with a where clause?)

推荐答案

INSERT INTO myTable(columns...) Select values... WHERE NOT EXISTS (SELECT * FROM myTable WHERE pk_part1 = value1, AND pk_part2 = value2)

修改: 阅读马丁斯链接后,如果可以接受,最好的解决方案是:

After reading martins link, If admit, that the best solution is:

BEGIN TRY INSERT INTO myTable(columns...) values( values...) END TRY BEGIN CATCH IF ERROR_NUMBER() <> 2627 RAISERROR etc END CATCH;

更多推荐

我该如何在不存在的地方插入?

本文发布于:2023-10-18 10:43:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1503999.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不存在   我该   地方   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!