从 MS SQL 的某个范围中获取随机值?

发布于 2024-11-06 05:19:27 字数 226 浏览 0 评论 0原文

假设我的表可以包含从 000 到 999 的值(三位数字且小于 1000),

其中一些值已填充。假设当前我的表有

000,002,005,190 (001,004,003,006,..189,191,..,999 可以插入表中)

并且这些值是随机分配的 000 和 002 在表中,但 001 尚未在表中。

我怎样才能获得可以插入表中的值。

Let suppose my table can have values from 000 to 999 (three digits and less than 1000)

Some of this values are filled. Let's suppose currently my table has

000,002,005,190 (001,004,003,006,..189,191,..,999 can be inserted into table)

and these values are randomly allocated 000 and 002 is in table but 001 is not in table yet.

How can I get the values that I can insert into table yet.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

守护在此方 2024-11-13 05:19:27
DECLARE @t TABLE
(VALUE CHAR(3))

INSERT @t
VALUES
('000'),('002'),('005'),('190')


;WITH rnCTE
AS
(
    SELECT -1 + ROW_NUMBER() OVER (ORDER BY TYPE, number, name) AS rn
    FROM master.dbo.spt_values
)
SELECT RIGHT('000' + CAST( rn AS VARCHAR(11)),3)
FROM rnCTE
WHERE NOT EXISTS    (   SELECT 1 FROM @t 
                        WHERE VALUE = rn
                    )
AND rn < 1000

编辑

此查询的工作原理是从系统表 (master.dbo.spt_values) 生成可能数字的完整列表,该表保证在 CTE 内包含超过 1000 行<代码>rnCTE。将 -1 添加到 ROW_NUMBER 中,使值从 0 而不是 1 开始。

外部查询用零填充要显示的数字,仅返回源数据中不存在且小于 1000 的数字。

DECLARE @t TABLE
(VALUE CHAR(3))

INSERT @t
VALUES
('000'),('002'),('005'),('190')


;WITH rnCTE
AS
(
    SELECT -1 + ROW_NUMBER() OVER (ORDER BY TYPE, number, name) AS rn
    FROM master.dbo.spt_values
)
SELECT RIGHT('000' + CAST( rn AS VARCHAR(11)),3)
FROM rnCTE
WHERE NOT EXISTS    (   SELECT 1 FROM @t 
                        WHERE VALUE = rn
                    )
AND rn < 1000

EDIT

This query works by generating the complete list of possible numbers from a system table (master.dbo.spt_values) which is guaranteed to contain more than 1000 rows inside the CTE rnCTE. -1 is added to ROW_NUMBER to have the values start at 0 rather than 1.

The outer query zero pads the numbers for display, returning only those which are not in the source data and are less than 1000.

水中月 2024-11-13 05:19:27
DECLARE @t TABLE(id INT)
INSERT INTO @t (id) 
VALUES 
(1),(19),(3)

;WITH numbers AS (
SELECT ROW_NUMBER() OVER(ORDER BY o.object_id,o2.object_id) RN FROM sys.objects o
CROSS JOIN sys.objects o2
),  NotExisted AS(
SELECT * FROM numbers WHERE RN NOT IN (SELECT ID FROM @t)
AND RN<1000)
SELECT TOP 1 RN FROM NotExisted ORDER BY NEWID()
DECLARE @t TABLE(id INT)
INSERT INTO @t (id) 
VALUES 
(1),(19),(3)

;WITH numbers AS (
SELECT ROW_NUMBER() OVER(ORDER BY o.object_id,o2.object_id) RN FROM sys.objects o
CROSS JOIN sys.objects o2
),  NotExisted AS(
SELECT * FROM numbers WHERE RN NOT IN (SELECT ID FROM @t)
AND RN<1000)
SELECT TOP 1 RN FROM NotExisted ORDER BY NEWID()
日暮斜阳 2024-11-13 05:19:27

您必须编写 T-SQL 来首先查询并找到差距。没有现成的 SQL 可以直接为您提供差距。

You will have to write a T-SQL to first query and find the gaps. There is no ready made SQL that will give you the gaps directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文