2013-03-17 88 views
0

我必须声明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

试试这个

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) 
+0

抛出错误'消息156,Level 15,State 1,Line 2 关键字'set'附近的语法不正确。' – Andrey 2013-03-17 22:17:55

+0

check updat ed回答 – 2013-03-17 22:19:21

+0

不认识这部分'x.clienid = c.cliendid' – Andrey 2013-03-17 22:21:51