Replace Into in SQL Server 2008

前端 未结 1 1789
独厮守ぢ
独厮守ぢ 2021-01-14 05:26

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

$DB->query(\"replace into periodetojour(idperiode,idjour,heure)
            values(\'\".addsl         


        
相关标签:
1条回答
  • 2021-01-14 06:16

    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)");
    

    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
                -- [...]
    
    0 讨论(0)
提交回复
热议问题