加法更新插入
在 Microsoft SQL Server 2008 中您将如何编写以下内容?
IF EXISTS(SELECT * FROM Table WHERE Something=1000)
UPDATE Table SET Qty = Qty + 1 WHERE Something=1000
ELSE
INSERT INTO Table(Something,Qty) VALUES(1000,1)
How would you write the following in Microsoft SQL Server 2008?
IF EXISTS(SELECT * FROM Table WHERE Something=1000)
UPDATE Table SET Qty = Qty + 1 WHERE Something=1000
ELSE
INSERT INTO Table(Something,Qty) VALUES(1000,1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,使用 SQL Server 2008 中新的
merge
命令会发现:这并不那么简单,因为合并对于合并更大的集合(而不仅仅是单个记录)来说更有效。否则,您可以尝试更新记录,如果没有更新记录,则插入一条记录:
Using the new
merge
command in SQL Server 2008 would as far as I can gather be:That not so simple, as the merge is more efficient for merging bigger sets, not just a single record. Otherwise you can just attempt to update the record, and insert one if no records were updated: