REPLACE INTO 查询是好的做法吗? [英] Is a REPLACE INTO query good practice?

查看:62
本文介绍了REPLACE INTO 查询是好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个 SQL 查询来更新或创建数据库条目(如果某个客户尚不存在).我在互联网上找到了当前的解决方案:

I needed an SQL query that would update or create a database entry (if a certain customer doesn't exist yet). I found the current solution on the internet:

command.CommandText = "REPLACE INTO [Resource_Tracer].[dbo].[Customer](CustomerName, CustomerID) VALUES (@CustomerName, @CustomerID)"

由于我没有看到它经常使用并且实际上以前从未听说过它,这真的是我想要的解决方案,还是应该手动执行此操作?

Since I don't see it used a lot and actually never heard of it before, is this really the solution I want, or should I do this manually?

推荐答案

建议的 REPLACE INTOON DUPLICATE KEY 替代方案都是来自 MySQL 变体的非标准 SQL.因此,您是否使用它取决于 a) 您是否正在使用 MySQl 和 b) 您是否想绑定到该变体.

Both REPLACE INTO and the ON DUPLICATE KEY alternative suggested are non standard SQL from the MySQL variant. So whether you use it depends on a) whether you're using MySQl and b) whether you want to be tied to that variant.

ANSI SQL 定义了更标准的 MERGE 语法,如果它在您的平台上实现

ANSI SQL defines a MERGE syntax that is more standard, if it is implemented on your platform

这篇关于REPLACE INTO 查询是好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆