如: 
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) 

解决方案 »

  1.   

    如果B表的时间大于给定的时间,则将A表里的状态(state)改为1你AB表怎么关联的?A.id=B.id ?
      

  2.   

    如果B表的时间大于给定的时间,则将A表里的状态(state)改为1 你AB表怎么关联的?A.id=B.id ?嗯,是根据ID进行关联的
      

  3.   

    这是我写的
    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 时发生语法错误。 
      

  4.   

    传入参数设为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)
      

  5.   

    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]>@EditDateOK???
      

  6.   

    BEGIN
    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的方法可行!
      

  7.   

    jiezi316  试了的,不报错但也没有结果chouto  说的情况暂时不考虑。lovehongyun 说的方法和我想的差不多,但是那个rdate时间不知怎么取啊。
    我取的运行时报错
      

  8.   

    SELECT @gettime=rdate FROM B where rdate between  @startDate and  @endDate 
      

  9.   


    rdate取他干啥?他就是个字段..直接update即可.你不就是为了判断是大于或小于传入时间吗?
      

  10.   

    直接两条语句ok?否则的话.你要循环了.一条记录一条记录的比CREATE PROCEDURE PEditOrderMenu 

    @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)
      

  11.   


    当我执行时,给个时间的参数。然后要取出B表的时间和给出的时间进行判断。如果B表的时间大于给定的时间,则将A表里的状态(state)改为1,否则改为0。=======
    你不就是说判断大小.然后更新a表的状态字段吗-_-!
      

  12.   

    update a set state=(select (case when rdate>@IDate then 1 else 0 end) from b) where id=b.id
      

  13.   

    汗,我的没有结果?
    也许是我没有理解到你的意思吧
    我的过程是
    找出B表中时间范围在@StartDate和@EndDate之间,并且[RDate]>@EditDate 
    对应的ID,并将A表中对应这些ID的记录都更新为1.
    应该没有啥子问题嘛
      

  14.   

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

  15.   

    嗯,给定时间和B表里的时间进行比较,B表里的时间有可能为空。然后根据结果修改A表里的状态 呵呵
    ===============================对啊.我写的就是这样的(把大于传入日期的更新一次.小于传入日期的更新一次.),
    可能为空的肯定就不管了..如果要管的话你得单独处理一下.
      

  16.   

    我仔细看了下估计你是这个意思
    Update A Set [State]=1 From A Join B On A.[ID]=B.[ID]
    Where B.[RDate]>@指定的时间你默认的就是0,所以更新为0的就可以不更新了吧OK???
    为了提高效率,尽量避免子查询和IN语句,
      

  17.   

    如果B表的时间大于给定的时间,则将A表里的状态(state)改为1,否则改为0。 
    更新A表的条件呢??
      

  18.   

    呵呵,谢谢各位了啊。
    刚才想了下,
    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的状态
      

  19.   

    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
      

  20.   

    update a set state=(select (case when (SELECT top(1) rdate FROM B where rdate between  @startDate and @endDate)>@EditDate then 1 else 0 end) from b) where id=b.id
    修正一下
      

  21.   

    update a set state= 
    (
        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 
      

  22.   

    -_-b更简单了
    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
      

  23.   

    如果先不考虑时间空的话,一条语句就搞定。
    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)
      

  24.   

    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为什么会把以前的状态也改了呢。
      

  25.   

    意思是传入二参数,
    一个@startTime, 一个@endTime
    如果 b 中存在一条记录的 rdate 介于 上二参数之间则,更新 a 表对应记录的 state 为 1?是不是这个意思。如果不是请说明。 看了半天只能理解成这样。