MySQL具有非常有用但适当的REPLACE INTOSQL命令。
这可以很容易地在SQL Server 2005中模拟?
开始一个新的事务,先执行a Select()然后再执行UPDATEor或INSERTand COMMIT总是很麻烦,尤其是在应用程序中执行该操作时,因此始终保留该语句的2个版本。
我想知道是否有一种简单且通用的方法将这种功能实现到SQL Server 2005中?
问题来源于stack overflow
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
--try an update update tablename set field1 = 'new value', field2 = 'different value', ... where idfield = 7
--insert if failed if @@rowcount = 0 and @@error = 0 insert into tablename ( idfield, field1, field2, ... ) values ( 7, 'value one', 'another value', ... ) 如果是更新,则减少为一个IO操作,如果是插入则为两个。
MS Sql2008 merge从SQL:2003标准引入:
merge tablename as target using (values ('new value', 'different value')) as source (field1, field2) on target.idfield = 7 when matched then update set field1 = source.field1, field2 = source.field2, ... when not matched then insert ( idfield, field1, field2, ... ) values ( 7, source.field1, source.field2, ... )
你好,我是AI助理
可以解答问题、推荐解决方案等
评论
全部评论 (0)