邹老大来帮帮我,各位高手也来帮帮我,急着要结果:(
我有两个表A与B
A表为价格表,B表为调价表
A表有ItemID,CustomID,Price
B表有ItemID,CustomID,Price,BeginDate,EndDate
我通过单据往B表里面录入调价的信息
现需要有一个自动执行的存储过程
根据B表BeginDate往A表往写入ItemID,CustomID,Price,如果A表中存在历史Price则更新Price
另外再根据B表的EndDate来执行更新A表,原则是找到B表中BeginDate最新的记录来更新A表中Price
每天更新一次
问题点数:100、回复次数:9Top
1 楼wofan(我烦)回复于 2004-12-03 22:45:48 得分 0
忘了一条,B表里面有主键IDTop
2 楼wofan(我烦)回复于 2004-12-03 22:49:55 得分 0
a表同样有一IDTop
3 楼hdhai9451(☆新人类☆)回复于 2004-12-03 23:16:25 得分 0
因為你B表有個ID主鍵,如果沒有了這個主鍵,那麼是不是存在有相同的ItemID,CustomID,Price,BeginDate,EndDate?
Top
4 楼hdhai9451(☆新人类☆)回复于 2004-12-03 23:18:24 得分 0
如果你的A表沒有主鍵,經過多次插入更新後,那麼ItemID,CustomID,Price可能會產生重復的記錄。
是不是還有什麼約束條件呢?
Top
5 楼zjcxc(邹建)回复于 2004-12-04 08:49:18 得分 100
--写一个更新处理的存储过程
create proc p_process
as
declare @dt datetime
set @dt=convert(char(10),getdate(),120) --去掉时间影响,只比较日期部分
--得到最新的调价信息
select b.ItemID,b.CustomID,Price=max(b.Price)
into #t
from B表 b,(
select ItemID,CustomID,BeginDate=max(BeginDate)
from B表
where BeginDate<=@dt and EndDate>=@dt
group by ItemID,CustomID
)b1 where b.BeginDate<=@dt and b.EndDate>=@dt
and b.ItemID=b1.ItemID and b.CustomID=b1.CustomID
and b.BeginDate=b1.BeginDate
group by b.ItemID,b.CustomID
--更新已经存在的价格
update a set Price=b.Price
from A表 a,#t b
where a.ItemID=b.ItemID and a.CustomID=b.CustomID
--插入A表中没有的
insert A(ItemID,CustomID,Price)
select ItemID,CustomID,Price
from #t b
where not exists(
select * from A表
where ItemID=b.ItemID and CustomID=b.CustomID)
go
Top
6 楼zjcxc(邹建)回复于 2004-12-04 08:51:14 得分 0
企业管理器
--管理
--SQL Server代理
--右键作业
--新建作业
--"常规"项中输入作业名称
--"步骤"项
--新建
--"步骤名"中输入步骤名
--"类型"中选择"Transact-SQL 脚本(TSQL)"
--"数据库"选择执行命令的数据库
--"命令"中输入要执行的语句:
exec p_process
--确定
--"调度"项
--新建调度
--"名称"中输入调度名称
--"调度类型"中选择你的作业执行安排
--如果选择"反复出现"
--点"更改"来设置你的时间安排
然后将SQL Agent服务启动,并设置为自动启动,否则你的作业不会被执行
设置方法:
我的电脑--控制面板--管理工具--服务--右键 SQLSERVERAGENT--属性--启动类型--选择"自动启动"--确定.Top
7 楼Gxzhs(搭错车)回复于 2004-12-04 09:17:14 得分 0
邹大侠真是高,又学到一招...Top
8 楼wofan(我烦)回复于 2004-12-04 09:37:26 得分 0
先谢谢,没想到邹老大周六都起来这么早
我真是惭愧Top
9 楼zjcxc(邹建)回复于 2004-12-04 10:13:06 得分 0
周六上班,不起来不行Top




