如:
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)
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)
IF EXISTS(SELECT * FROM SYSOBJECTS WHERE NAME='PEditOrderMenu' and TYPE='P')
DROP PROCEDURE PEditOrderMenu
GO
CREATE PROCEDURE PEditOrderMenu
(
@editDate datetime, --更新截止时间
@startDate datetime, --当日最早时间
@endDate datetime --当日最晚时间
)
AS
SET NOCOUNT ON
DECLARE @gettime datetime BEGIN
set @gettime = 'SELECT rdate FROM B where rdate between '+ @startDate + 'and ' + @endDate
END
IF @gettime > @editDate
UPDATE A set State=1 WHERE ID in (SELECT ID FROM B) and sDate between @startDate and @endDate
ELSE
UPDATE A set State=0 WHERE ID in (SELECT ID FROM B) and sDate between @startDate and @endDate
GO exec PEditOrderMenu '2008-09-19 10:30:00','2008-09-19 00:00:01','2008-09-19 23:59:59'
这是我写的,执行时报:从字符串转换为 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)
@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]>@EditDateOK???
set @gettime = 'SELECT rdate FROM B where rdate between '+ @startDate + 'and ' + @endDate
END
IF @gettime > @editDate 'SELECT rdate FROM B where rdate between '+ @startDate + 'and ' + @endDate 这句SQL查出来的不一定只有一个记录吧,如果赋给一个变量,会不会出错?
感觉lovehongyun的方法可行!
我取的运行时报错
rdate取他干啥?他就是个字段..直接update即可.你不就是为了判断是大于或小于传入时间吗?
(
@IDate datetime)
AS
begin
update a set state状态= 1 where id in (select id from b where rdate时间>@IDate)
update a set state状态= 0 where id in (select id from b where rdate时间 <@IDate)
当我执行时,给个时间的参数。然后要取出B表的时间和给出的时间进行判断。如果B表的时间大于给定的时间,则将A表里的状态(state)改为1,否则改为0。=======
你不就是说判断大小.然后更新a表的状态字段吗-_-!
也许是我没有理解到你的意思吧
我的过程是
找出B表中时间范围在@StartDate和@EndDate之间,并且[RDate]>@EditDate
对应的ID,并将A表中对应这些ID的记录都更新为1.
应该没有啥子问题嘛
===============================对啊.我写的就是这样的(把大于传入日期的更新一次.小于传入日期的更新一次.),
可能为空的肯定就不管了..如果要管的话你得单独处理一下.
Update A Set [State]=1 From A Join B On A.[ID]=B.[ID]
Where B.[RDate]>@指定的时间你默认的就是0,所以更新为0的就可以不更新了吧OK???
为了提高效率,尽量避免子查询和IN语句,
更新A表的条件呢??
刚才想了下,
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 给两个参数,一个是每天的开始时间如:2008-09-19 00:00:01 ,一个是给定的时间:2008-09-19 10:00:00如果今天A表编号为1的人员在10点之前打卡的话,在B表里rdate就有记录,如果没来rdate就没有记录,这时就要修改A表里编号为1的状态
修正一下
(
select
(
case when
(
SELECT top(1) isnull(rdate,getdate() FROM B where rdate between @startDate and @endDate --取得中间值 因为你这里取得是个集合,所以用top 1, isnull 如果是取空值了就取当前时间
)>@EditDate then 1 else 0 end --判断取出的值是否大于EditDate
) from b
) where id=b.id
update a set state= (case when (select rdate from b where rdate between @startDate and @endDate ) is null then 1 else 0 end)
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()+1insert 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()+1update 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)
where id=b.id为什么会把以前的状态也改了呢。
一个@startTime, 一个@endTime
如果 b 中存在一条记录的 rdate 介于 上二参数之间则,更新 a 表对应记录的 state 为 1?是不是这个意思。如果不是请说明。 看了半天只能理解成这样。