打印连续的数字

发布于 2024-10-27 09:00:58 字数 45 浏览 1 评论 0原文

我如何使用单个 sql 查询打印连续的数字..如 1 , 2 , 3 ...

How do i print consecutive numbers in using a single sql query .. like 1 , 2 , 3 ...

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

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

发布评论

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

评论(7

巾帼英雄 2024-11-03 09:00:58
;with cte
as 
(
select 1 [sequence]
union all
select [sequence]+1 from cte where [sequence]<100
)
select * from cte

尝试,通过使用通用类型表达式我们可以做到......它的工作原理

;with cte
as 
(
select 1 [sequence]
union all
select [sequence]+1 from cte where [sequence]<100
)
select * from cte

try,By using Common type expression we can do it.....Its working

瑶笙 2024-11-03 09:00:58

不确定我是否正确理解了您的问题,但如果您只想PRINT连续的数字,我不明白为什么您不能执行以下操作:

DECLARE @a INT
SET @a = 1
WHILE @a <= 10
BEGIN
PRINT @a
SET @a += 1
END

Not sure if I've understood your question correctly, but if you just want to PRINT consecutive numbers I don't see why you couldn't do the following:

DECLARE @a INT
SET @a = 1
WHILE @a <= 10
BEGIN
PRINT @a
SET @a += 1
END
月野兔 2024-11-03 09:00:58

你想获取sql结果集中的行号吗?尝试以下操作:

SET @line = 0; 
SELECT @line := @line + 1, some_field FROM table_name;

对于 SQL Server:http://msdn.microsoft.com/en -us/library/ms186734.aspx

SELECT FirstName, LastName, ROW_NUMBER() OVER(ORDER BY SalesYTD DESC) AS 'Row Number',     SalesYTD, PostalCode 

FROM Sales.vSalesPerson

WHERE TerritoryName IS NOT NULL AND SalesYTD <> 0;

Are you trying to get the line number in the sql result set? try the following:

SET @line = 0; 
SELECT @line := @line + 1, some_field FROM table_name;

For SQL Server: http://msdn.microsoft.com/en-us/library/ms186734.aspx

SELECT FirstName, LastName, ROW_NUMBER() OVER(ORDER BY SalesYTD DESC) AS 'Row Number',     SalesYTD, PostalCode 

FROM Sales.vSalesPerson

WHERE TerritoryName IS NOT NULL AND SalesYTD <> 0;
孤芳又自赏 2024-11-03 09:00:58

像...

  select
            (a3.id + a2.id + a1.id + a0.id) as id
        FROM 
        /* create the tables to be used for the cartesian join */
        (
            select 0 id UNION ALL 
            select 1 UNION ALL
            select 2 UNION ALL
            select 3 UNION ALL
            select 4 UNION ALL
            select 5 UNION ALL
            select 6 UNION ALL
            select 7 UNION ALL
            select 8 UNION ALL
            select 9
        ) as a0,    
        (
            select 0 id UNION ALL
            select 10 UNION ALL
            select 20 UNION ALL
            select 30 UNION ALL
            select 40 UNION ALL
            select 50 UNION ALL
            select 60 UNION ALL
            select 70 UNION ALL
            select 80 UNION ALL
            select 90  
        ) as a1, 
        (
            select 0 id UNION ALL
            select 100 UNION ALL
            select 200 UNION ALL
            select 300 UNION ALL
            select 400 UNION ALL
            select 500 UNION ALL
            select 600 UNION ALL
            select 700 UNION ALL
            select 800 UNION ALL
            select 900
        ) as a2, 
        (
            select 0 id UNION ALL
            select 1000 UNION ALL
            select 2000 
        ) as a3 
    order by id asc ;

Something like...

  select
            (a3.id + a2.id + a1.id + a0.id) as id
        FROM 
        /* create the tables to be used for the cartesian join */
        (
            select 0 id UNION ALL 
            select 1 UNION ALL
            select 2 UNION ALL
            select 3 UNION ALL
            select 4 UNION ALL
            select 5 UNION ALL
            select 6 UNION ALL
            select 7 UNION ALL
            select 8 UNION ALL
            select 9
        ) as a0,    
        (
            select 0 id UNION ALL
            select 10 UNION ALL
            select 20 UNION ALL
            select 30 UNION ALL
            select 40 UNION ALL
            select 50 UNION ALL
            select 60 UNION ALL
            select 70 UNION ALL
            select 80 UNION ALL
            select 90  
        ) as a1, 
        (
            select 0 id UNION ALL
            select 100 UNION ALL
            select 200 UNION ALL
            select 300 UNION ALL
            select 400 UNION ALL
            select 500 UNION ALL
            select 600 UNION ALL
            select 700 UNION ALL
            select 800 UNION ALL
            select 900
        ) as a2, 
        (
            select 0 id UNION ALL
            select 1000 UNION ALL
            select 2000 
        ) as a3 
    order by id asc ;
蓝眸 2024-11-03 09:00:58
DECLARE @NUM INT, @COUNT INT , @NUM1 INT , @SPACE INT
SET @NUM=1 SET @COUNT=1000 SET @NUM1=0 

WHILE(@NUM<=@COUNT)

BEGIN

WHILE (@NUM1<=@NUM) 

BEGIN
DECLARE @STORE VARCHAR(MAX)
SET @NUM1=@NUM1+1 --1
SET @SPACE = (@COUNT-@NUM) --3

SET @STORE=ISNULL(@STORE,'')+SPACE(1)+CAST(@NUM1 AS VARCHAR(MAX))--1
PRINT (SPACE(@SPACE)+@STORE)

IF(@NUM<=@COUNT)
BEGIN
SET @NUM=@NUM+1
END
END


SET @NUM1=0

END

以三角形格式打印数字

DECLARE @NUM INT, @COUNT INT , @NUM1 INT , @SPACE INT
SET @NUM=1 SET @COUNT=1000 SET @NUM1=0 

WHILE(@NUM<=@COUNT)

BEGIN

WHILE (@NUM1<=@NUM) 

BEGIN
DECLARE @STORE VARCHAR(MAX)
SET @NUM1=@NUM1+1 --1
SET @SPACE = (@COUNT-@NUM) --3

SET @STORE=ISNULL(@STORE,'')+SPACE(1)+CAST(@NUM1 AS VARCHAR(MAX))--1
PRINT (SPACE(@SPACE)+@STORE)

IF(@NUM<=@COUNT)
BEGIN
SET @NUM=@NUM+1
END
END


SET @NUM1=0

END

To print numbers in triangular format

成熟稳重的好男人 2024-11-03 09:00:58
declare @digits table (id int,value int )
insert @digits values (1,80),(1,90),(1,100),(1,200),(1,210),(1,9),(1,10),(1,12),
(2,8),(2,9),(2,11),(2,12),
(3,2),(3,4),(3,5),(3,7)

select distinct id,stuff(convert(varchar(max),
(
    select 
        ',' + case 
            when min(value) = max(value) then convert(varchar(10), min(value)) 
            else convert(varchar(10), min(value)) + '-' + convert(varchar(10), max(value)) 
        end
    from (select row_number() over (partition by id order by id,value) as seq, value,id from @digits) data
    where data.id = s.id
    group by (value/10) - seq
    for xml path('')
)), 1, 1, '') as result from @digits s
declare @digits table (id int,value int )
insert @digits values (1,80),(1,90),(1,100),(1,200),(1,210),(1,9),(1,10),(1,12),
(2,8),(2,9),(2,11),(2,12),
(3,2),(3,4),(3,5),(3,7)

select distinct id,stuff(convert(varchar(max),
(
    select 
        ',' + case 
            when min(value) = max(value) then convert(varchar(10), min(value)) 
            else convert(varchar(10), min(value)) + '-' + convert(varchar(10), max(value)) 
        end
    from (select row_number() over (partition by id order by id,value) as seq, value,id from @digits) data
    where data.id = s.id
    group by (value/10) - seq
    for xml path('')
)), 1, 1, '') as result from @digits s
醉态萌生 2024-11-03 09:00:58
declare @digits table (id int,value int )
insert @digits values (1,1),(1,2),(1,3),(1,6),(1,8),(1,9),(1,10),(1,12),
(2,8),(2,9),(2,11),(2,12),
(3,2),(3,4),(3,5),(3,7)

select distinct id,stuff(convert(varchar(max),
(
    select 
        ',' + case 
            when min(value) = max(value) then convert(varchar(10), min(value)) 
            else convert(varchar(10), min(value)) + '-' + convert(varchar(10), max(value)) 
        end
    from (select row_number() over (partition by id order by id,value) as seq, value,id from @digits) data
    where data.id = s.id
    group by value - seq
    for xml path('')
)), 1, 1, '') as result from @digits s
declare @digits table (id int,value int )
insert @digits values (1,1),(1,2),(1,3),(1,6),(1,8),(1,9),(1,10),(1,12),
(2,8),(2,9),(2,11),(2,12),
(3,2),(3,4),(3,5),(3,7)

select distinct id,stuff(convert(varchar(max),
(
    select 
        ',' + case 
            when min(value) = max(value) then convert(varchar(10), min(value)) 
            else convert(varchar(10), min(value)) + '-' + convert(varchar(10), max(value)) 
        end
    from (select row_number() over (partition by id order by id,value) as seq, value,id from @digits) data
    where data.id = s.id
    group by value - seq
    for xml path('')
)), 1, 1, '') as result from @digits s
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文