基于select的更新语句

时间:2013-03-17 22:14:59

标签: sql sql-server-2008 select sql-update

我有陈述

update Clients
set StatusID= 4
from (select c.clientid
from Clients c
where CategCode = 'CH' and StatusID in (1,2,6) and DATEDIFF(YEAR,dob,GETDATE())>5)

它让我犯错误

Msg 102, Level 15, State 1, Line 5
Incorrect syntax near ')'.

你看到会导致这个错误的原因吗?

我试图使用此声明,但它为所有客户端设置状态4

update Clients
set StatusID= 4
WHERE EXISTS( 
select clientid,DOB,DATEDIFF(YEAR,dob,GETDATE())
from Clients 
where CategCode = 'CH' and StatusID in (1,2,6) and DATEDIFF(YEAR,dob,GETDATE())>5

1 个答案:

答案 0 :(得分:1)

试试这个

update c
set c.StatusID= 4
from Clients c
where CategCode = 'CH' and StatusID in (1,2,6) 
and DATEDIFF(YEAR,dob,GETDATE())>5

您的第二个查询应该是

update c
set StatusID= 4
from Clients c
WHERE EXISTS( 
select 1
from Clients x
where CategCode = 'CH' and StatusID in (1,2,6)  and x.clientid = c.clientid)