SQLServer 对现有更新
我们计划将应用程序从 Sybase SQL Anywhere 转换为 SQL Server。
在 SA,我们使用很多“现有更新”,即通过主键检查行是否存在。 如果为真,则更新(如果我执行更新子句,则以同样的方式)。 如果没有,则插入。
SQL Server 支持这样的东西吗?
We are planning to convert an application from Sybase SQL Anywhere to SQL Server.
At SA we use a lot of "on existing update", that checks by primary key, if line exists.
If true, is update (in same way if I was executing update clause).
If not, it inserts.
Does SQL Server supports something like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您将使用 SQL Server 2008 及更高版本,则可以使用 MERGE 命令。您可以在此处找到说明:
http://technet.microsoft .com/en-us/library/bb510625(SQL.100).aspx
如果您使用较旧的 SQL,Jeremiah Clark's 建议的解决方案埃里克·萨斯会好的。
If you will use SQL Server 2008 and later, you can use MERGE command. You can find description here:
http://technet.microsoft.com/en-us/library/bb510625(SQL.100).aspx
If you use older SQL, Jeremiah Clark's solution suggested by Erick Sasse will be ok.
SQL Server 具有高端 RDBMS、存储过程、触发器、UDF 等的所有功能...您可能知道 MS SQL Server 完全是从 Sybase 分支出来的,因此它们确实有共同的根源。
我不确定我是否完全理解你的问题,当然你可以有一个包含一些逻辑的 SQL 语句,并根据某些条件或如果已经找到记录来执行 INSERT 或 UPDATE,TSQL 是 SQL Server 的 SQL 方言并支持此功能还有更多。
如果您确实对某份声明有具体问题或疑问,请向我们展示您目前在南澳的声明。
SQL Server has all the features of an high-end RDBMS, Stored Procedures, Triggers, UDF and so on... as you probably know MS SQL Server was branched out exactly from Sybase so they do have common roots.
I am not sure I get your question completely, of course you can have a SQL Statement which contains some logic and does INSERT or UPDATE depending on some conditions or if a record was already found, TSQL is the SQL dialect of SQL Server and supports this and much more.
If you really have a specific question or doubt on a statement please show us your statement as you have it right now in SA.
AFAIK 在一个命令中不支持这一点。我发现的最好方法是遵循 Jeremiah Clark 的提示:尝试更新,然后检查受影响的记录数。如果它为零,那么我插入:
AFAIK there is no support for this in one command. And the best way I found was follow Jeremiah Clark's tip: try to update, then check for the number of affected records. If it was zero, then I insert: