开发者社区 问答 正文

SQL Server 2005实现MySQL替换成吗?

MySQL具有非常有用但适当的REPLACE INTOSQL命令。

这可以很容易地在SQL Server 2005中模拟?

开始一个新的事务,先执行a Select()然后再执行UPDATEor或INSERTand COMMIT总是很麻烦,尤其是在应用程序中执行该操作时,因此始终保留该语句的2个版本。

我想知道是否有一种简单且通用的方法将这种功能实现到SQL Server 2005中?

问题来源于stack overflow

展开
收起
保持可爱mmm 2019-11-15 14:47:44 541 分享
分享
版权
举报
1 条回答
写回答
取消 提交回答
  • --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, ... )

    2019-11-15 14:48:07 举报
    赞同 评论

    评论

    全部评论 (0)

    登录后可评论
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等