Monday, 28 November 2011

Merge Example

MERGE tblsample AS TARGET
USING
      (SELECT Id,
              Name,
              Age,
              Sex FROM tblsample1) AS Mer ON TARGET.id=Mer.id

 WHEN MATCHED AND
      
       (TARGET.Name<>mer.Name)or
       (TARGET.Age<>mer.Age)or
       (TARGET.Sex<>mer.Sex)

 THEN UPDATE
 SET    TARGET.Name=mer.Name,
        TARGET.Age=mer.Age,
        TARGET.Sex=mer.Sex
   
 WHEN NOT MATCHED BY TARGET
 THEN INSERT
        (
         Name,
         Age,
         Sex
         )
 VALUES
        ( mer.Name,
          mer.Age,
          mer.Sex
         );

No comments:

Post a Comment