在 SQL Server 2008 中替换为

Replace Into in SQL Server 2008(在 SQL Server 2008 中替换为)
本文介绍了在 SQL Server 2008 中替换为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试进行迁移,但这个查询有问题:

I try to do a migration but i have problem with this query :

$DB->query("replace into periodetojour(idperiode,idjour,heure)
            values('".addslashes($idperiode)."','2','".addslashes($mardi)."')");

我看到 REPLACE INTO 不能在 SQL Server 2008 中使用,我必须使用 MERGE INTO

I saw that REPLACE INTO can't be used in SQL Server 2008 and that i hav to use MERGE INTO

我的问题是我没有找到任何使用 MERGE INTO 工作的查询,所以我可能没有很好地使用它.您知道如何使用 MERGE INTO 更改它吗?是否有义务在 SQL Server 2008 中更改它?

My problem is that i don't find any of my query which is working by using MERGE INTO so i am probably not using it well. Do you have any idea how can i change it with MERGE INTO and is it an obligation to change it in SQL Server 2008 ?

感谢您的回答.

推荐答案

在您的情况下,MERGE 语句如下所示:

In your case, the MERGE statement would look like this:

$DB->query("MERGE INTO periodetojour dst 
            USING ( 
              SELECT '".addslashes($idperiode)."' idperiode, 
                     '2' idjour, 
                     '".addslashes($mardi)."' heure 
            ) src  
            ON src.idperiode = dst.idperiode  
            WHEN MATCHED THEN UPDATE SET  
              dst.idjour = src.idjour, 
              dst.heure = src.heure  
            WHEN NOT MATCHED THEN INSERT (idperiode, idjour, heure)  
              VALUES(src.idperiode, src.idjour, src.heure)");

这是假设 idperiode 是您的主键.如果主键由 (idperiode, idjour) 组成,则必须调整 ON 子句以及 WHEN MATCHED THEN UPDATE SET 相应的子句:

This is assuming that idperiode is your primary key. If the primary key is composed of (idperiode, idjour) you'll have to adapt the ON clause as well as the WHEN MATCHED THEN UPDATE SET clause accordingly:

            -- [...]
            ON src.idperiode = dst.idperiode  
            AND src.idjour = dst.idjour
            WHEN MATCHED THEN UPDATE SET  
              dst.heure = src.heure
            -- [...]

这篇关于在 SQL Server 2008 中替换为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)