我正在嘗試更新同一列中的兩個值,在一個行程中更新不同行上的同一個表。其中 CategoryID 是主要與次要的 DefaultForAxisID 標記。
在BEFORE 和 AFTER示例中,我需要同時設定以下兩項以避免錯誤。
- DefaultforAxisID = 2 其中 ProcedureCodeID = 195837 和 CategoryID = 21
- DefaultforAxisID = null WHERE ProcedureCodeID = 195837 和 CategoryID = 22
換句話說,我需要將 DefaultforAxisID 交換為 CategoryID 21 和 22。
當我執行Fig A 時,出現以下錯誤(Fig B),但它確實更新了列。我可能可以通過第二次更新來跟進它,但我只是對它拋出錯誤感到不舒服,因此正在探索替代方案。
圖A。
UPDATE ProcedureCodeCategory
SET DefaultForAxisID = null
WHERE CategoryID = 22 and ProcedureCodeID = 195837
圖乙:
[Code: 50000, SQL State: S0001] Clearing an existing default is not permitted. Please select a new deafault for this procedure if this is no longer valid. [Script position: 1795 - 1805]
當我弄清楚如何實作這一點時,我將在資料庫中創建一個存盤程序,并將其合并到一個 Python 函式中。
前
| 程式代碼類別ID | 程式代碼ID | 類別編號 | 是繼承的 | 默認軸 ID |
|---|---|---|---|---|
| 998743 | 195837 | 21 | 錯誤的 | (空值) |
| 998509 | 195837 | 22 | 錯誤的 | 2 |
| 998742 | 195837 | 314 | 錯誤的 | (空值) |
| 998510 | 195837 | 316 | 錯誤的 | 3 |
后
| 程式代碼類別ID | 程式代碼ID | 類別編號 | 是繼承的 | 默認軸 ID |
|---|---|---|---|---|
| 998743 | 195837 | 21 | 錯誤的 | 2 |
| 998509 | 195837 | 22 | 錯誤的 | (空值) |
| 998742 | 195837 | 314 | 錯誤的 | (空值) |
| 998510 | 195837 | 316 | 錯誤的 | 3 |
uj5u.com熱心網友回復:
一個簡單的 case 運算式可以很容易地在這里作業。
update YourTable
set DefaultforAxisID = case CategoryID when 21 then 2 when 22 then NULL else DefaultforAxisID end
where CategoryID in (21, 22)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/361939.html
標籤:sql sql-server 查询语句
下一篇:當列不匹配時,SQLServer在INSERT/SELECT上鎖定......但僅在SQLServerManagementStudio中
