SQL:对 INSERT 和 UPDATE 使用相同的字符串?
我一直在使用的 INSERT 语法是这样的
INSERT INTO TableName VALUES (...)
我一直在使用的 UPDATE 语法是
UPDATE TableName SET ColumnName=Value WHERE ...
所以在我的所有代码中,我必须生成 2 个字符串,这会导致类似这样的结果
insertStr = "(27, 'John Brown', 102)";
updateStr = "ID=27, Name='John Brown', ItemID=102";
,然后单独使用它们
"UPDATE TableName SET " + updateStr + " WHERE ID=27 " +
"IF @@ROWCOUNT=0 "+
"INSERT INTO TableName VALUES (" + insertStr + ")"
当我我正在处理大约有 30 列的表。
难道我们不能只生成一个字符串来同时用于 INSERT 和 UPDATE 吗?
例如。 在 UPDATE 语句上使用 insertStr 或在 INSERT 语句上使用 updateStr,还是采用全新的方式?
The INSERT syntax I've been using is this
INSERT INTO TableName VALUES (...)
The UPDATE syntax I've been using is
UPDATE TableName SET ColumnName=Value WHERE ...
So in all my code, I have to generate 2 strings, which would result in something like this
insertStr = "(27, 'John Brown', 102)";
updateStr = "ID=27, Name='John Brown', ItemID=102";
and then use them separately
"UPDATE TableName SET " + updateStr + " WHERE ID=27 " +
"IF @@ROWCOUNT=0 "+
"INSERT INTO TableName VALUES (" + insertStr + ")"
It starts bothering me when I am working with tables with like 30 columns.
Can't we generate just one string to use on both INSERT and UPDATE?
eg. using insertStr above on UPDATE statement or updateStr on INSERT statement, or a whole new way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我认为你需要一种全新的方法。 您可以接受SQL 注入。 向我们提供一些示例代码,说明如何获取数据输入并将语句发送到数据库。
替代文本 http://goose.ycp.edu /~weddins/440/S09%20IFS440%20Bobby%20Drop%20Tables.PNG
I think you need a whole new approach. You are open to SQL Injection. Provide us with some sample code as to how you are getting your data inputs and sending the statements to the database.
alt text http://goose.ycp.edu/~weddins/440/S09%20IFS440%20Bobby%20Drop%20Tables.PNG
据我所知,您所描述的内容在 ANSI SQL 或我所知道的任何扩展中都是不可能的。 然而,我最熟悉 MySQL,它可能完全取决于您使用的 RDBMS。 例如,MySQL 具有“INSERT ... ON DUPLICATE KEY UPDATE ...”语法,该语法与您在那里发布的类似,并将 INSERT 查询与 UPDATE 查询组合在一起。 好处是您将两个可能的操作组合到一个查询中,但是,查询的 INSERT 和 UPDATE 部分确实是不同的。
一般来说,这种事情可以通过应用程序中的 ORM 层进行抽象。 就原始 SQL 而言,我会对任何按照您描述的方式工作的语法感兴趣。
As far as I'm aware, what you're describing isn't possible in ANSI SQL, or any extension of it that I know. However, I'm mostly familiar with MySQL, and it likely depends completely upon what RDBMS you're using. For example, MySQL has "INSERT ... ON DUPLICATE KEY UPDATE ... " syntax, which is similar to what you've posted there, and combines an INSERT query with an UPDATE query. The upside is that you are combining two possible operations into a single query, however, the INSERT and UPDATE portions of the query are admittedly different.
Generally, this kind of thing can be abstracted away with an ORM layer in your application. As far as raw SQL goes, I'd be interested in any syntax that worked the way you describe.
一些 DBMS 有一个扩展来执行此操作,但为什么不提供一个函数来为您执行此操作呢? 我们之前实际上已经这样做过。
我不确定您使用的是什么语言,但它可能有关联数组,您可以在其中编写类似的内容:
并且,如果它没有关联数组,您可以使用多个基于整数的字符串数组来模拟它们。
在
upsert()
函数中,我们只是构造了一个字符串(update
,如果update
失败则构造insert
)并将其传递给我们的 DBMS。 我们将主键与其他字段分开,因为这使得更新语句的构造更加容易(主键列位于 where 子句中,其他列只是设置)。上述调用的结果将产生以下 SQL(我们对失败的
update
进行了不同的检查,但在本示例中我已将您的@@rowcount
放入其中):这是一种对我们来说效果很好的解决方案。 毫无疑问还有其他人。
Some DBMS' have an extension to do this but why don't you just provide a function to do it for you? We've actually done this before.
I'm not sure what language you're using but it's probably got associative arrays where you can wrote something like:
and, if it doesn't have associative arrays, you can emulate them with multiple integer-based arrays of strings.
In our
upsert()
function, we just constructed a string (update
, theninsert
if theupdate
failed) and passed it to our DBMS. We kept the primary keys separate from our other fields since that made construction of the update statement a lot easier (primary key columns went in the where clause, other columns were just set).The result of the calls above would result in the following SQL (we had a different check for failed
update
but I've put your@@rowcount
in for this example):That's one solution which worked well for us. No doubt there are others.
好吧,不发表声明怎么样? 您可能需要研究 ORM 来为您处理这个问题......
Well, how about no statements? You might want to look into an ORM to handle this for you...
一些数据库具有专门执行此操作的专有扩展。
我同意 INSERT 和 UPDATE 的语法可以更加一致,但这只是现在的事实——现在不会改变。 对于许多场景,最好的选择是“全新方式”:使用对象关系映射库(甚至是像 .NET DataSets 这样的弱茶层)来抽象出差异,并且不再担心低级 SQL句法。 当然,这并不是每个应用程序都可行的选择,但它允许您构造或更新对象、调用 Save 方法并让库为您计算出 SQL 语法。
Some databases have proprietary extensions that do exactly this.
I agree that the syntax of INSERT and UPDATE could be more consistent, but this is just a fact of life now -- it ain't gonna change now. For many scenarios, the best option is your "whole new way": use an object-relational mapping library (or even a weak-tea layer like .NET DataSets) to abstract away the differences, and stop worrying about the low-level SQL syntax. Not a viable option for every application, of course, but it would allow you to just construct or update an object, call a Save method and have the library figure out the SQL syntax for you.
如果你仔细想想,INSERT 和 UPDATE 是完全相同的事情。 它们将字段名称映射到值,但 UPDATE 有一个过滤器。
通过创建关联数组(其中键是字段名称,值是要分配给该字段的值),您就拥有了映射。 您只需根据 INSERT 或 UPDATE 将其转换为正确的字符串格式。
您只需要创建一个函数来根据给定的参数处理转换。
If you think about it, INSERT and UPDATE are exactly the same thing. They map field names to values, except the UPDATE has a filter.
By creating an associative array, where the key is the field name and the value is the value you want to assign to the field, you have your mapping. You just need to convert it to a the proper string format depending on INSERT or UPDATE.
You just need to create a function that will handle the conversion based on the parameters given.
SQL Server 2008:
MySQL:
SQL Server 2008:
MySQL: