 WITH latestsignature AS
          (SELECT sig.savedate,
                  sig.recipe,
                  sig.recipesignatureid
           FROM aps.t_recipe_signature sig
           WHERE NOT EXISTS (SELECT 1
                             FROM aps.t_recipe_signature sig2
                             WHERE sig2.recipe = sig.recipe
                               AND sig2.savedate > sig.savedate
                            )
          )
 UPDATE aps.t_recipe r
 SET lastsignature = ls.recipesignatureid
 FROM latestsignature ls
 WHERE r.recipeid = ls.recipe;
 
 WITH lateststatushistory AS
	(SELECT sh.creationdatetime,
			sh.recipe,
			sh.recipestatushistoryid
	 FROM aps.t_recipe_status_history sh
     WHERE sh.recipe = p_recipeid
		AND NOT EXISTS (SELECT 1
						FROM aps.t_recipe_status_history sh2
                        WHERE sh.recipe = sh2.recipe
							AND sh2.creationdatetime > sh.creationdatetime
                       )
             )
UPDATE aps.t_recipe r
SET laststatushistory = lsh.recipestatushistoryid
FROM lateststatushistory lsh
WHERE r.recipeid = lsh.recipe;
 
 