SQL - 在插入时生成循环编号

时间:2017-10-31 12:43:01

标签: sql sql-server round-robin

我正在部署一个现有的定制Windows服务(C#)多个实例,这些实例将从单个队列表中读取。

队列基于以下简单的SQL表。

Record-Id (int auto id)

Added-Date (Date)

Added-By (Text)

Data-To-be-Processed (Text)

**Pool-Number (int)**

如果给定最大池大小,我如何为每个插入创建一个循环序列号?这里我使用的池大小为3(这可以是硬编码的)。 e.g。

1 | 31/10/2014 | DATA | Pool 1

2 | 31/10/2014 | DATA | Pool 2

3 | 31/10/2014 | DATA | Pool 3

4 | 31/10/2014 | DATA | Pool 1

5 | 31/10/2014 | DATA | Pool 2

6 | 31/10/2014 | DATA | Pool 3

7 | 31/10/2014 | DATA | Pool 1

我考虑过使用一个Sequence表并在每个插件上递增它,并在达到最大池大小时将其重置为1。

TbSeq

  dbSeq (int) (Will contain 1-3 depending last insert)

有更好的方法吗?

3 个答案:

答案 0 :(得分:2)

如果您可以使用RecordId作为辅助,那么您可以使用Modulo (%)

select *, 1+((RecordId-1)%3) as Pool
from t

rextester演示:http://rextester.com/WNEIQM50851

返回:

+----------+------------+-------------------+------+
| RecordId | AddedDate  | DataToBeProcessed | Pool |
+----------+------------+-------------------+------+
|        1 | 2014-10-31 | DATA              |    1 |
|        2 | 2014-10-31 | DATA              |    2 |
|        3 | 2014-10-31 | DATA              |    3 |
|        4 | 2014-10-31 | DATA              |    1 |
|        5 | 2014-10-31 | DATA              |    2 |
|        6 | 2014-10-31 | DATA              |    3 |
|        7 | 2014-10-31 | DATA              |    1 |
+----------+------------+-------------------+------+

您可以将其添加为计算列(持久可选,但建议使用)

alter table t
  add Pool as (1+((RecordId-1)%3)) persisted;

答案 1 :(得分:0)

我不会依赖RecordId 本身。它可能有差距。进行真正循环的最简单方法是使用row_number()和模运算:

select 1 + (row_number() over (order by id) - 1) % 3 as poolnum
from t;

如果您知道RecordId没有间隙,那么您可以使用它。使用RecordId更有效,因为您可以在一行内完成全部计算,甚至可以添加计算列:

alter table t add poolnum as (1 + (row_number() over (order by id) - 1) % 3)

答案 2 :(得分:0)

您可以将SECQUENCE用作:

CREATE TABLE MyTable (
    Record_Id INT IDENTITY(1,1), Added_Date DATE,
    Added_By VARCHAR(50), Data_To_be_Processed VARCHAR(50), Pool_Number INT);
GO    
CREATE SEQUENCE Pool
AS INT
START WITH 1
MINVALUE 1
MAXVALUE 3
CYCLE
GO

INSERT INTO MyTable VALUES
('2014-10-31', 'DATA', 'Pool', NEXT VALUE FOR Pool),
('2014-10-31', 'DATA', 'Pool', NEXT VALUE FOR Pool),
('2014-10-31', 'DATA', 'Pool', NEXT VALUE FOR Pool),
('2014-10-31', 'DATA', 'Pool', NEXT VALUE FOR Pool),
('2014-10-31', 'DATA', 'Pool', NEXT VALUE FOR Pool),
('2014-10-31', 'DATA', 'Pool', NEXT VALUE FOR Pool),
('2014-10-31', 'DATA', 'Pool', NEXT VALUE FOR Pool),
('2014-10-31', 'DATA', 'Pool', NEXT VALUE FOR Pool),
('2014-10-31', 'DATA', 'Pool', NEXT VALUE FOR Pool);

SELECT *
FROM MyTable;

结果:

+-----------+---------------------+----------+----------------------+-------------+
| Record_Id |     Added_Date      | Added_By | Data_To_be_Processed | Pool_Number |
+-----------+---------------------+----------+----------------------+-------------+
|         1 | 31.10.2014 00:00:00 | DATA     | Pool                 |           1 |
|         2 | 31.10.2014 00:00:00 | DATA     | Pool                 |           2 |
|         3 | 31.10.2014 00:00:00 | DATA     | Pool                 |           3 |
|         4 | 31.10.2014 00:00:00 | DATA     | Pool                 |           1 |
|         5 | 31.10.2014 00:00:00 | DATA     | Pool                 |           2 |
|         6 | 31.10.2014 00:00:00 | DATA     | Pool                 |           3 |
|         7 | 31.10.2014 00:00:00 | DATA     | Pool                 |           1 |
|         8 | 31.10.2014 00:00:00 | DATA     | Pool                 |           2 |
|         9 | 31.10.2014 00:00:00 | DATA     | Pool                 |           3 |
+-----------+---------------------+----------+----------------------+-------------+