开发者社区> 问答> 正文

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 514 0
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
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
SQL Server在电子商务中的应用与实践 立即下载
GeoMesa on Spark SQL 立即下载
原生SQL on Hadoop引擎- Apache HAWQ 2.x最新技术解密malili 立即下载

相关镜像