基于服务的数据库的序列号

Sequence number for Service-Based Database

我想弄清楚,如果我的本地数据库(基于服务的数据库)中的任何行被删除,我想给第二行(删除后的下一个)已删除行的索引,所有行都返回相同的步骤当然,后续行在插入时保持原始行顺序也很重要。同样的问题我从这里开始:How to setup auto increment for Service-Based Database but now I'm looking for a different way of solving. I want this number of row for some calculations to read line. It soon became clear that it is better not to do it this way as I’ve tried it here Reset auto increment 看来我需要数字序列。所以在这种情况下索引号表示序列号,例如:

  1. line 1
  2. line 2
  3. line 3
  4. line 4

如果第 2 行被删除我想要这个结果:

  1. line 1
  2. line 3
  3. line 4

如果所有行都删除,然后插入新行就变成了1。新的第一行 而不是 5。新的第一行,因为它现在可以与索引一起使用。

我认为你应该使用自动递增字段(比如 ID)到 "keep original order of lines as it was inserted"。并使用 VIEW 例如 select 来自 table 的行,使用 ROW_NUMBER() 函数模拟序列号

CREATE VIEW T_WITH_ROW_ID  
AS
SELECT ROW_NUMBER() OVER(ORDER BY ID) as ROW_ID,
       T.*
       FROM T

并将其用作

SELECT * FROM T_WITH_ROW_ID