CREATE PROCEDURE MIG_FILE_IMAGES_Sp AS   
BEGIN  
   
  -- To DELETE deleted records from FILE_IMAGES
  DELETE MIG
  FROM MIG_FILE_IMAGES  MIG
  LEFT JOIN FILE_IMAGES 
  ON	FILE_IMAGES.DSET_KEY = MIG.DSET_KEY AND 
		FILE_IMAGES.DSET_REVISION = MIG.DSET_REVISION AND 
		FILE_IMAGES.OBJECT_TYPE = MIG.OBJECT_TYPE AND 
		FILE_IMAGES.OBJECT_KEY = MIG.OBJECT_KEY AND 
		FILE_IMAGES.OBJECT_REVISION = MIG.OBJECT_REVISION
 WHERE FILE_IMAGES.DSET_KEY IS NULL

 -- To INSERT NEW records from FILE_IMAGES

 INSERT INTO MIG_FILE_IMAGES ( DSET_KEY,  
         DSET_REVISION,  
         OBJECT_TYPE,  
         OBJECT_KEY,  
         OBJECT_REVISION,  
         THUMBNAIL_TYPE,  
         OUT_OF_DATE,  
         CREATED_ON,  
         UPDATED_ON  
        )  
 SELECT FILE_IMAGES.DSET_KEY,  
	    FILE_IMAGES.DSET_REVISION,  
	    FILE_IMAGES.OBJECT_TYPE,  
	    FILE_IMAGES.OBJECT_KEY,  
	    FILE_IMAGES.OBJECT_REVISION,  
	    FILE_IMAGES.THUMBNAIL_TYPE,  
	    FILE_IMAGES.OUT_OF_DATE,  
	    FILE_IMAGES.CREATED_ON,  
	    FILE_IMAGES.UPDATED_ON  
 FROM FILE_IMAGES 
 LEFT JOIN MIG_FILE_IMAGES 
	ON	FILE_IMAGES.DSET_KEY = MIG_FILE_IMAGES.DSET_KEY AND 
		FILE_IMAGES.DSET_REVISION = MIG_FILE_IMAGES.DSET_REVISION AND 
		FILE_IMAGES.OBJECT_TYPE = MIG_FILE_IMAGES.OBJECT_TYPE AND 
		FILE_IMAGES.OBJECT_KEY = MIG_FILE_IMAGES.OBJECT_KEY AND 
		FILE_IMAGES.OBJECT_REVISION = MIG_FILE_IMAGES.OBJECT_REVISION --AND 
		--FILE_IMAGES.CREATED_ON = MIG_FILE_IMAGES.CREATED_ON AND 
		--FILE_IMAGES.UPDATED_ON = MIG_FILE_IMAGES.UPDATED_ON
 WHERE MIG_FILE_IMAGES.DSET_KEY IS NULL

 -- To UPDATE NEW records from FILE_IMAGES
 UPDATE MIG 
		SET MIG.THUMBNAIL_TYPE = FIMG.THUMBNAIL_TYPE,
			MIG.OUT_OF_DATE = FIMG.OUT_OF_DATE,
			MIG.CREATED_ON = FIMG.CREATED_ON,
			MIG.UPDATED_ON = FIMG.UPDATED_ON
 FROM MIG_FILE_IMAGES MIG
 JOIN FILE_IMAGES FIMG 
	ON	FIMG.DSET_KEY = MIG.DSET_KEY AND 
		FIMG.DSET_REVISION = MIG.DSET_REVISION AND 
		FIMG.OBJECT_TYPE = MIG.OBJECT_TYPE AND 
		FIMG.OBJECT_KEY = MIG.OBJECT_KEY AND 
		FIMG.OBJECT_REVISION = MIG.OBJECT_REVISION AND 
		(	FIMG.CREATED_ON <> MIG.CREATED_ON OR 
			FIMG.UPDATED_ON <> MIG.UPDATED_ON
		)
 
END  
  
