2013-03-17 77 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