如何使用单个SQL查询打印连续数字...如1,2,3 ......
答案 0 :(得分:2)
;with cte
as
(
select 1 [sequence]
union all
select [sequence]+1 from cte where [sequence]<100
)
select * from cte
尝试,通过使用Common类型表达式我们可以做到.....它的工作
答案 1 :(得分:1)
不确定我是否正确理解了您的问题,但如果您只想PRINT
个连续数字,我不明白为什么您不能执行以下操作:
DECLARE @a INT
SET @a = 1
WHILE @a <= 10
BEGIN
PRINT @a
SET @a += 1
END
答案 2 :(得分:0)
您是否尝试获取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;
答案 3 :(得分:0)
像...一样的东西。
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 ;
答案 4 :(得分:0)
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
以三角形格式打印数字
答案 5 :(得分:0)
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
答案 6 :(得分:0)
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