2012-07-05 31 views
0

我更熟悉Oracle,因此SQL Server 2008的語法正在拋出我。我試圖執行與查詢相當於多行的更新使用select子查詢進行TSQL批量更新

update Table 
set type = 'typeA' 
where id in 
(select id from Table where type='typeB') 

我收到以下錯誤:

Msg 512, Level 16, State 1, Procedure Assigned_To_Email, Line 19 
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. 

搜索一個TSQL具體的解決辦法我試過下面的語法卻收到同樣的錯誤。

update a 
set type = 'typeA' 
from Table a 
join Table b 
on a.id = b.id 
where b.type='typeB' 

我讀過子查詢的更新應該可以工作,但這不是我的經驗。這裏有更基本的失蹤嗎? 感謝您的幫助!

+0

您收到的錯誤不是來自您在此處包含的查詢。我認爲你已經簡化了你的實際查詢。我的猜測是,您使用子查詢來獲取您分配給鍵入的值或您與類型比較的值,並且它是由於它返回多行而產生錯誤的子查詢。 – 2012-07-05 15:40:59

+0

是的,子查詢返回多個行,但這不是(不應該)是一個錯誤......但是,你是對的,這個問題與一個觸發器有關,該觸發器正在執行一個由於多行而出錯的語句。我感謝您的評論,因爲它將我引向實際問題。謝謝。 – McArthey 2012-07-06 16:12:01

回答

2

您的更新語句沒有使用子查詢或加入。

update Table 
set type = 'typeA' 
where type = 'typeB'