SQL Server:跟踪配方数据库中的更改

SQL Server:跟踪配方数据库中的更改,sql,sql-server-2012-express,Sql,Sql Server 2012 Express,我有一个配方,大约有30个值,需要记录任何时候使用配方制作的产品,其中任何属性都不同于上次更改配方时的属性。以下是我的示例代码: 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

我有一个配方,大约有30个值,需要记录任何时候使用配方制作的产品,其中任何属性都不同于上次更改配方时的属性。以下是我的示例代码:

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
这是最有效的方法还是有更好的方法?大约有100个食谱,每隔几周就会更新一次


SQL Server版本是SQL Server 2012 Express。

示例数据和所需结果确实会让您的问题更容易理解。每个配方有两个关键字段(CatalogNo,timestamp)和30列(real1,real2,…real30)。数据从另一个系统发送到SQL,只有当它与该CatalogNo的SQL中的最后一个条目不同时,我才需要将其保存到SQL历史记录表中。