Replace Into in SQL Server 2008

I am trying to migrate, but I have a problem with this query:

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

I saw what REPLACE INTOcannot be used in SQL Server 2008 and what I want to useMERGE INTO

My problem is that I cannot find any of my queries that works with help MERGE INTO, so I probably don't use it very well. Do you have any ideas how I can change it using MERGE INTO and is it required to change it in SQL Server 2008?

Thanks for your reply.

+3
source share
1 answer

In your case, the operator MERGEwill 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 :

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

All Articles