2015-08-21 145 views
1

當我運行此查詢;SQL - 更新不起作用?

UPDATE dbo.Account 
SET new_AccountTypeIdName = IIF(new_Existing = 1 , 'Existing','Potential') 
Where new_AccountTypeIdName = 'Member' 
AND new_Existing IN (0,1) 

它只是改變了什麼,是'new_AccountTypeIdName = '會員' 到 '存在',不管'new_existing = 1或0。

任何人都可以指向正確的方向嗎? IM使用SQL Server 2012

感謝

+1

運行這個命令:'SELECT * FROM帳戶,new_AccountTypeIdName ='成員'AND new_Existing IN(0,1)'查看預期的更新行。我懷疑會有空白的結果。 – i486

+0

@ i486,我跑你的查詢,我得到了結果 – AndroidAL

回答

0

嘗試此查詢

UPDATE dbo.Account 
SET new_AccountTypeIdName = case when new_Existing = 1 then 'Existing' else 'Potential' end 
Where new_AccountTypeIdName = 'Member' 
AND new_Existing IN (0,1) 
+0

我跑你的查詢相同的結果,它只是說它影響了一行,但農民的所有行都受到影響? – AndroidAL

+0

然後檢查您的數據和條件 –

+0

雖然此代碼可能解決OP的問題,但解釋幾句話將大大有助於讓每個人都更容易理解此答案。 – Thom

1

拆分查詢到2個簡單:

UPDATE dbo.Account 
    SET new_AccountTypeIdName = 'Existing' 
    Where new_AccountTypeIdName = 'Member' AND new_Existing = 1 

UPDATE dbo.Account 
    SET new_AccountTypeIdName = 'Potential' 
    Where new_AccountTypeIdName = 'Member' AND new_Existing = 0 
+0

同樣的結果?當我在CRM Online上查看 – AndroidAL

+0

「相同的結果」 - 即只有一個查詢正在工作? – i486