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

关于UPDATE语句
我现在有两个表   PROCESS   和BACK
表PROCESS中有字段OVERTIME和LIMITDATE,表BACK中有BACKDATE字段
这两个表是关于ID关联的。
我现在想用UPDATE语句修改PROCESS表的OVERTIME标志,把凡是BACK表中的BACKDATE大于PROCESS中的LIMITDATE的记录的OVERTIME设置为2.该怎么写呢?
我用如下的语句可是overtime全成2了,把所有的记录都修改了.我用的SQLSERVER2005

我写的语句:
UPDATE         POURPROCESS
SET                             OverTime   =   2
FROM                   PourProcess   AS   a   INNER   JOIN
                                            PourBack   AS   b   ON   a.CallID   =   b.CallID   AND   CONVERT(datetime,   a.DealLimitDate,   120)   <   CONVERT(datetime,   b.BackDate,   120)

------解决方案--------------------
a.DealLimitDate 和b.BackDate是什么类型??
------解决方案--------------------
if object_id( 'process ') is not null
drop table process
go
create table process(ID int, overtime varchar(230),limidate datetime)
insert process select 1001, '55 ', '2002-4-15 '
union all select 1009, '57 ', '2002-10-15 '
union all select 1004, '85 ', '2002-9-15 '
go
if object_id( 'back ') is not null
drop table back
go
create table back(ID int,CallID int,backdate datetime)
insert back select 1,1001, '2002-6-15 '
union all select 2,1004, '2002-9-17 '
union all select 3,1009, '2002-9-15 '
go
select * from process
select * from back
go
update process set overtime = 2 where ID in (select process.ID
from process inner join back on process.ID = back.CallID and process.limidate < back.backdate)
------解决方案--------------------
create table tmp5
(id int identity(1,1) ,overtime int,limitdate datetime)
create table tmp6
(id int identity(1,1) ,backdate datetime)

insert into tmp5(overtime,limitdate) values( '1 ', '2006-12-31 ')
insert into tmp5(overtime,limitdate) values( '1 ', '2006-05-31 ')
insert into tmp5(overtime,limitdate) values( '1 ', '2006-08-31 ')
insert into tmp5(overtime,limitdate) values( '1 ', '2006-11-30 ')
insert into tmp5(overtime,limitdate) values( '1 ', '2006-01-28 ')

insert into tmp6(backdate) values( '2006-12-22 ')
insert into tmp6(backdate) values( '2006-12-22 ')
insert into tmp6(backdate) values( '2006-09-22 ')
insert into tmp6(backdate) values( '2006-05-22 ')
insert into tmp6(backdate) values( '2006-02-22 ')

update tmp5
set overtime=2
from tmp6,tmp5
where tmp5.id=tmp6.id and backdate> limitdate
select * tmp5

不知道是楼主的意思不?
等待好的方法,学习中!~~
------解决方案--------------------
--try


update A set OverTime=2
from PourProcess A, PourBack B
where A.CallID=B.CallID and CONVERT(datetime, a.DealLimitDate, 120) < CONVERT(datetime, b.BackDate, 120)
------解决方案--------------------
if object_id( 'process ') is not null
drop table process
go
create table process(ID int, overtime varchar(230),limidate datetime)
insert process select 1001, '55 ', '2002-4-15 '