在SQL Server 2008中替换为

Cha*_*don 3 mysql migration sql-server-2008

我尝试进行迁移,但我对此查询有问题:

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

我看到它REPLACE INTO不能在SQL Server 2008中使用,而且我可以使用它MERGE INTO

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

感谢您的回答.

Luk*_*der 6

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

$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)");
Run Code Online (Sandbox Code Playgroud)

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

            -- [...]
            ON src.idperiode = dst.idperiode  
            AND src.idjour = dst.idjour
            WHEN MATCHED THEN UPDATE SET  
              dst.heure = src.heure
            -- [...]
Run Code Online (Sandbox Code Playgroud)