2

在 PostgrSQL 上,我这样做没问题:

CREATE SEQUENCE serial_olw START 1;
update collections_elements set elementorder=(nextval('serial_olw')-1) WHERE collections_elements.collectionid=1;
drop sequence serial_olw;

示例:1,2,3,4,5,6...

在 MS-SQL Server 2008 上没有函数 SEQUENCE... 所以我尝试了这个:

DECLARE @i int
SET @i = 0

WHILE @i<44
    BEGIN
    UPDATE collections_elements set elementorder=(@i) WHERE collections_elements.collectionid=1
        SET @i=@i+1
    END

但我在那个循环中没有成功......

示例:43,43,43,43,43...

任何解决方案的想法?

4

4 回答 4

3
Update t
Set t.elementorder = t.RowID
From
(
    Select ROW_NUMBER() Over(Order by collectionid) as RowID, * From collections_elements
)t

SQL小提琴

于 2012-05-25T10:04:54.360 回答
1
update T
set elementorder = rn
from
  (
    select elementorder,
           row_number() over(order by (select 0)) as rn
    from collections_elements
    where collectionid = 1
  ) T

SE-数据

于 2012-05-25T10:51:33.667 回答
0

尝试这样的事情 - 内联变量增量:

DECLARE @i int 
SET @i = 0

WHILE @i<44 
BEGIN
    UPDATE collections_elements 
    SET @i = elementorder = @i + 1 
    WHERE collections_elements.collectionid=1 
END
于 2013-05-10T10:11:05.110 回答
0

此脚本是一个示例。尝试这样的事情 - 内联变量增量:

DECLARE @i int 
SET @i = 1 

BEGIN
    UPDATE Questionnaire 
    SET prioritize = @i 
    ,  @i = @i +1 
    WHERE documentTypeCode=2 and groupCode=02
END
于 2020-01-08T11:45:27.300 回答