日期:2014-05-19  浏览次数:20543 次

|M| 有什么办法让表中某字段根据理一表中的字段自动更新
如我有表
Product
ID       Name       Price
1         Apple     12

PriceUpdate
ID     ProductID     Date               Price    
2       1                     2007-7-1       15

上面的Product为产品表
PriceUpdate为价格更新表
要实现的是
每过一天查询ProductUpdate表中
当Proudct   ID   =   ProductID  
然后Date时间为当前时间的时候
更新PriceUpdate中的Price到Product中的Price

谢谢
也就是一个表根据更一个表自动更新他的记录内容

------解决方案--------------------
--创建Job,调度执行如下SQL

update Product set Price=B.Price
from Product as a
inner join PriceUpdate as b on A.id=B.ProductID
where datediff(d,B.[date],getdate())=0


------解决方案--------------------
楼上都说了
------解决方案--------------------
--创建Job,作业调度每天一次,调度执行如下SQL:


update Product set Price=B.Price
from Product as a
inner join PriceUpdate as b on A.id=B.ProductID
where datediff(d,B.[date],getdate())=0

------解决方案--------------------
update Product set Price=B.Price
from Product as a
inner join PriceUpdate as b on A.id=B.ProductID and b.B.[date]
=(select max(date) from PriceUpdate where ProductID=b.ProductID)


如果保证不会修改历史记录,可以考虑用触发器


------解决方案--------------------
用触发器不合适,因为追加的PriceUpdate中的date不一定是当天的日期