循环遍历SQL Server中的表并更新特定计数索引处的列

时间:2018-04-05 12:28:41

标签: sql-server database tsql

我正在尝试使用下面的值更新列值,并且我正在使用循环来尝试实现此功能。请参阅我正在尝试更新的字段的屏幕截图。

即我正在尝试将第二行详细信息更新为第一行参考。

https://i.stack.imgur.com/GD7V2.png

DECLARE @CTRw INT
SET @CTRw = 2

WHILE @CTRw <= 600
BEGIN
    UPDATE [SBSA_Loaded] 
    SET Reference  = LEFT(Details, 50) 
    WHERE ID = @CTRw

    --DELETE FROM [SBSA_Loaded] WHERE id = @CTRw

    PRINT @CTRw 

    -- SELECT id, details FROM [SBSA_Loaded] WHERE ID = @CTR
    SET @CTRw += 2
END

2 个答案:

答案 0 :(得分:3)

我知道领导可能就是答案。但我总是使用ID + 1

上的自我加入来做到这一点

我知道你在做什么,这将在没有循环的情况下完成:

步骤1 - 使用下一行更新参考:

Update main
set Reference = left(next.Details,50)
from SBSA_Loaded main
join SSBA_Loaded next on main.ID+1=next.ID

第2步 - 删除偶数行:

Delete from SBSA_Loaded
where ID % 2 = 0  --MOD looks at the remainder of division

答案 1 :(得分:1)

您可以使用modus %lead

执行此操作
declare @T table (pk int identity primary key, descr varchar(20), val 

varchar(20));
insert into @T (descr) VALUES ('odd description'), ('even description'), ('odd description'), ('even description'), ('odd description'), ('even description'), ('odd description')
                            , ('even description'), ('odd description'), ('even description'), ('odd description'), ('even description'), ('odd description'), ('even description')
                            , ('odd description'), ('even description'), ('odd description'), ('even description'), ('odd description'), ('even description'), ('odd description');
select *
     , pk % 2 as modus
from @T t;

with cte as 
( select *
       , lead(descr) over (order by pk) as leadDescr
  from @T t
)

update t 
set val = LEFT(leadDescr, 4)
from CTE t 
where pk % 2 = 1;

select *
from @T t;

pk          descr                val                  modus
----------- -------------------- -------------------- -----------
1           odd description      NULL                 1
2           even description     NULL                 0
3           odd description      NULL                 1
4           even description     NULL                 0
5           odd description      NULL                 1
6           even description     NULL                 0
7           odd description      NULL                 1
8           even description     NULL                 0
9           odd description      NULL                 1
10          even description     NULL                 0
11          odd description      NULL                 1
12          even description     NULL                 0
13          odd description      NULL                 1
14          even description     NULL                 0
15          odd description      NULL                 1
16          even description     NULL                 0
17          odd description      NULL                 1
18          even description     NULL                 0
19          odd description      NULL                 1
20          even description     NULL                 0
21          odd description      NULL                 1

pk          descr                val
----------- -------------------- --------------------
1           odd description      even
2           even description     NULL
3           odd description      even
4           even description     NULL
5           odd description      even
6           even description     NULL
7           odd description      even
8           even description     NULL
9           odd description      even
10          even description     NULL
11          odd description      even
12          even description     NULL
13          odd description      even
14          even description     NULL
15          odd description      even
16          even description     NULL
17          odd description      even
18          even description     NULL
19          odd description      even
20          even description     NULL
21          odd description      NULL