我有要求用不同的密鑰更新兩行的列值。約束是我想在單個查詢中做到這一點。在單個SQL查詢中交換同一表的列值
例如:
Coll1 Coll2
---------------
A 1
B 2
C 3
應該像使用case
表達
Coll1 Coll2
--------------
A 3
B 2
C 1
我有要求用不同的密鑰更新兩行的列值。約束是我想在單個查詢中做到這一點。在單個SQL查詢中交換同一表的列值
例如:
Coll1 Coll2
---------------
A 1
B 2
C 3
應該像使用case
表達
Coll1 Coll2
--------------
A 3
B 2
C 1
:
update t
set Coll2 = case when Coll1 = 'A' then 3 else 1 end
where Coll1 in ('A','C')
rextester演示:http://rextester.com/HUBDAP9516
返回:
+-------+-------+
| Coll1 | Coll2 |
+-------+-------+
| A | 3 |
| B | 2 |
| C | 1 |
+-------+-------+
更新參數化版本:
declare @key1 char(1) = 'A';
declare @key2 char(1) = 'C';
update t
set t.Coll2 = x.Coll2
from t
inner join t x
on t.Coll1 <> x.Coll1
and t.Coll1 in (@key1,@key2)
and x.Coll1 in (@key1,@key2)
rextester演示:http://rextester.com/PKQSAV63963
回報:
+-------+-------+
| Coll1 | Coll2 |
+-------+-------+
| A | 3 |
| B | 2 |
| C | 1 |
+-------+-------+
也許你的意思是一個單一的交易。無論哪種方式,我都不明白爲什麼 - 但既然這就是你想要的,這是一個簡單的方法。
declare @table table (Col1 char(1), Col2 int)
insert into @table
values
('A',1),
('B',2),
('C',3)
update @table
set
Col2 = case
when Col1 = 'A' then 3
when Col1 = 'C' then 1
end
where Col1 in ('A','C')
select * from @table
BEGIN TRANSACTION
UPDATE t SET Coll2 = 3 WHERE Coll1 = 'A'
UPDATE t SET Coll2 = 1 WHERE Coll1 = 'C'
COMMIT
我想試試下面的答案,而不硬編碼COL2
create table #t (col1 char(301), col2 int);
go
insert into #t (col1, col2)
values ('A', 1), ('B', 2), ('C', 3)
; with c as (
select col1, col2
from #t
)
update t
set t.col2 = c.col2
from #t t
inner join c
on abs(ascii(t.col1) - ascii(c.col1))=2
select * from #t
你會看看我提出的解決方案嗎? – Horaciux
這是你有價值觀切換的參數或子查詢的通用解決方案。
它也適用於con char數據,但需要對字符串操作進行一些調整。
declare @table table (Col1 char(1), Col2 int)
insert into @table
values
('A',1),
('B',2),
('C',3)
declare @swap1 char='A'
declare @swap2 char='C'
update @table
set col2 = (select sum(col2) from @table
where col1 in (@swap1,@swap2))-col2
where col1 in (@swap1,@swap2)
SqlZim是對的,但你爲什麼要在單個查詢中做到這一點? –
更合適的詞將是'單筆交易'。 – ggtffg
我剛剛添加了一個通用的解決方案,沒有硬編碼任何值 – Horaciux