I have a recipe with about 30 values and need to keep a log of anytime the product is made using a recipe where any attribute is different than the last time the recipe was changed. Here is my sample code:
if not exists
(select 1 from
--find most recent version of this recipe:
(select top 1 * from tbl_recipehistory
where catalogno='ipe' order by dts desc) as r
where r.value1=1 and r.value2=1 and r.value3=2, r.value(N-1)=1, value(n)=1)
begin
--the recipe has changed so save a copy with current time stamp
insert tbl_recipehistory (catalogno,dts,value1,value2,value3, value(n-1), value(n))
values('ipe',getdate(),1,1,2,...,1,1 )
end
Is this the most efficient method or is there a better way? There are about 100 recipes and updates will be made every few weeks.
SQL Server version is SQL Server 2012 Express.