2011-03-29 79 views
2

我如何使用一個SQL查詢打印連續的數字..像1,2,3 ...打印連續編號

+0

Ummmm ..我懷疑它是否可以完成。反正你的問題+1。 – 2011-03-29 15:08:23

+0

連續數字從什麼到什麼。你是什​​麼意思'打印'使用一個單一的查詢。 'PRINT'接受一個變量而不是查詢。 – 2011-03-29 15:14:56

+0

爲什麼地球上有這個問題得到3 upvotes? '打印'1,2,3 ......''符合目前的要求。 – 2011-03-29 16:16:18

回答

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; 
+0

看起來像MySQL語法。 – 2011-03-29 15:12:50

+0

哦,對不起,sql服務器似乎有一個類似的東西:ROW_NUMBER()http://msdn.microsoft.com/en-us/library/ms186734.aspx – 2011-03-29 15:15:19

+0

不在2000年它不。 SQL Server中有一種非常類似的技術來連接字符串。這可能也可能用於增加一個變量,但是您將依賴於正確大小的表格可用。 – 2011-03-29 15:17:07

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 ; 
1

不知道如果我理解正確你的問題,但如果你只是想要PRINT連續號碼我不明白你爲什麼不能做到以下幾點:

DECLARE @a INT 
SET @a = 1 
WHILE @a <= 10 
BEGIN 
PRINT @a 
SET @a += 1 
END 
0
DECLARE @NUM INT, @COUNT INT , @NUM1 INT , @SPACE INT 
SET @NUM=1 SET @COUNT=1000 SET @NUM1=0 

WHILE(@NUM<[email protected]) 

BEGIN 

WHILE (@NUM1<[email protected]) 

BEGIN 
DECLARE @STORE VARCHAR(MAX) 
SET @[email protected]+1 --1 
SET @SPACE = (@[email protected]) --3 

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

IF(@NUM<[email protected]) 
BEGIN 
SET @[email protected]+1 
END 
END 


SET @NUM1=0 

END 

要在三角格式

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

嘗試打印數量,通過使用普通型表達,我們可以做到這一點.....它的工作

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 
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