2017-04-17 105 views
0

這是我的示例和我的要求。我使用SQL Server 2012的如果存在SQL Server 2012減少位值

declare @nMyBitValue bigint = 32 

update MyTabl1 
set MyBitValue = case 
        when MyBitValue & @nMyBitValue <> 0 
         then MyBitValue - @nMyBitValue 
         else MyBitValue 
       end 
where Id = 1 

現有數據MyBitValue列是48,@nMyBitValue = 32

現在如果3234的值存在於MyBitValue中,那麼這個值應該從MyBitValue列中減去。

我減少了我的價值MyBitValue - @nMyBitValue。但是有沒有其他辦法可以減少我的價值?

+0

? –

+0

SQL Server 2012 –

+0

你的要求對我來說還不清楚! - 正如我所看到的:當'@ nMyBitValue'等於'0'時,你想更新'MyBitValue'字段!,這是嗎? ;) –

回答

0

恕我直言,不;我覺得沒有更好的方式來做到這一點,但你可以優化你這樣的代碼:

declare @nMyBitValue bigint = 32; 

update MyTabl1 
set MyBitValue = MyBitValue & [email protected] 
-- MyBitValue - @nMyBitValue -->> I thought this is better 
where Id = 1 
    and MyBitValue & @nMyBitValue <> 0; 

這將只更新行那些需要更新不是Id =1

+0

我問,而不是直接刪除使用減法運算符是否有任何其他方式來刪除該值 –

+0

而我回答,否;)。 –

+0

你已經將它直接設置爲這樣MyBitValue- @ nMyBitValue,但我不應該使用「 - 」 –

0

所有行嘗試下面的更新查詢

DECLARE @nMyBitValue bigint = 32 

UPDATE MyTabl1 
SET MyBitValue = CASE WHEN (MyBitValue > 0 AND @nMyBitValue > 0) 
         THEN MyBitValue - @nMyBitValue ELSE MyBitValue END 
WHERE Id = 1 
+0

我不應該直接使用「 - 」運算符來刪除。還有其他方法可以減少嗎? –

+0

你有沒有得到任何錯誤..? – Mansoor

0

得到的都是你使用SQL Server 2012或MySQL解決方案

declare @nMyBitValue bigint = 32 

update MyTabl1 
    set MyBitValue = 
     case 
      when MyBitValue & @nMyBitValue <> 0 then MyBitValue^@nMyBitValue 
      else MyBitValue 
     end 
    where Id = 1