日期:2014-05-18  浏览次数:20427 次

求一SQL存储过程,高人们来啊
如: 
A B 
id编号 | state状态 | sdate时间 id编号 | rdate时间 
  1 | 0 | 2008-09-19 00:00:00 1 | 2008-09-19 09:30:00 
  2 | 0 | 2008-09-19 00:00:00 2 | 2008-09-19 08:30:00 
  3 | 0 | 2008-09-19 00:00:00 3 | 2008-09-19 10:30:00 
  4 | 0 | 2008-09-19 00:00:00 4 | 2008-09-19 09:10:00 
  1 | 0 | 2008-09-20 00:00:00 1 |  
  2 | 0 | 2008-09-20 00:00:00 2 | 2008-09-20 08:30:00 
  3 | 0 | 2008-09-20 00:00:00 3 | 2008-09-20 10:30:00 
  4 | 0 | 2008-09-20 00:00:00 4 | 2008-09-20 09:10:00 

当我执行时,给个时间的参数。然后要取出B表的时间和给出的时间进行判断。如果B表的时间大于给定的时间,则将A表里的状态(state)改为1,否则改为0。 
B表里的时间有可能为空。(可能需要三个参数,一个给定的时间参数,还有两个是当日时间的开始和结束时间“当天 00:00:01 --当天 23:59:59)

------解决方案--------------------
传入参数设为DateTime类型

@IDate DateTime

直接比即可
rdate时间>@IDate
update a set state状态= 1 where id in (select id from b where rdate时间>@IDate)

rdate时间<@IDate
update a set state状态= 0 where id in (select id from b where rdate时间<@IDate)
------解决方案--------------------
Create Proc xxxxXXXXXXX
@editDate datetime,
@startDate datetime, 
@endDate datetime
AS
Update A Set State=1 From A Join B On A.[ID]=B.[ID]
Where B.[RDate] Between(@StartDate And @EndDate) And B.[RDate]>@EditDate



OK???
------解决方案--------------------
嗯,给定时间和B表里的时间进行比较,B表里的时间有可能为空。然后根据结果修改A表里的状态 

呵呵
===============================

对啊.我写的就是这样的(把大于传入日期的更新一次.小于传入日期的更新一次.),
可能为空的肯定就不管了..如果要管的话你得单独处理一下.


------解决方案--------------------
SQL code
update a set state=(select (case when (SELECT top(1) rdate,getdate() FROM B where rdate between  @startDate and @endDate)>@EditDate then 1 else 0 end) from b) where id=b.id

------解决方案--------------------
如果先不考虑时间空的话,一条语句就搞定。
create table a (
id int,
state char(1),
sdate datetime
)

create table b(
id int,
rdate datetime
)
insert into a
select 1,'0',getdate()
union all
select 2,'0',getdate()
union all
select 3,'0',getdate()
union all
select 4,'0',getdate()
union all
select 1,'0',getdate()+1
union all
select 2,'0',getdate()+1
union all
select 3,'0',getdate()+1
union all
select 4,'0',getdate()+1

insert into b
select 1,getdate()
union all
select 2,getdate()
union all
select 3,getdate()
union all
select 4,getdate()
union all
select 1,getdate()+1
union all
select 2,getdate()+1
union all
select 3,getdate()+1
union all
select 4,getdate()+1

update a set a.state=(case when convert(varchar(19),a.sdate,120)=convert(varchar(19),b.rdate,120) and b.rdate>getdate() then '1' else '0' end)
from a,b
where a.id=b.id and convert(varchar(8),a.sdate,120)=convert(varchar(8),b.rdate,120)