我會更復雜地撰寫我的命令列陳述句。如何通過使用回圈來做到這一點?
update refs set custom_7 = '';
update refs set custom_7='1' where custom_7 = '' limit 220 ;
update refs set custom_7='2' where custom_7 = '' limit 220 ;
update refs set custom_7='3' where custom_7 = '' limit 220 ;
...
update refs set custom_7='100' where custom_7 = '' limit 220 ;
多謝。
uj5u.com熱心網友回復:
如果有一列,如id,定義了要更新行的行的順序,請使用ROW_NUMBER()視窗函式對行進行排名并加入表:
WITH cte AS (SELECT *, ROW_NUMBER() OVER (ORDER BY id) rn FROM refs)
UPDATE refs r
INNER JOIN cte c ON c.id = r.id
SET r.custom_7 = (c.rn - 1) DIV 220 1
WHERE c.rn <= 100 * 220; -- remove the WHERE clause if there is actually no limit to the values of custom_7
如果沒有類似的列id,則可以ORDER BY id從 的OVER()子句中洗掉ROW_NUMBER(),但行將被任意更新。
查看簡化的演示。
uj5u.com熱心網友回復:
您可以嘗試這樣的事情(請將datatype(length)替換為 custom7 的型別)
DECLARE @count INT;
SET @count = 1;
WHILE @count<= 100
BEGIN
UPDATE refs SET custom_7 = CAST(@count AS **datatype(length)**) WHERE custom_7 = '' LIMIT 220;
SET @count = @count 1;
END;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/450608.html
