我如何使用一個SQL查詢打印連續的數字..像1,2,3 ...打印連續編號
打印連續編號
回答
你試圖讓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;
看起來像MySQL語法。 – 2011-03-29 15:12:50
哦,對不起,sql服務器似乎有一個類似的東西:ROW_NUMBER()http://msdn.microsoft.com/en-us/library/ms186734.aspx – 2011-03-29 15:15:19
不在2000年它不。 SQL Server中有一種非常類似的技術來連接字符串。這可能也可能用於增加一個變量,但是您將依賴於正確大小的表格可用。 – 2011-03-29 15:17:07
喜歡的東西...
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 ;
不知道如果我理解正確你的問題,但如果你只是想要PRINT
連續號碼我不明白你爲什麼不能做到以下幾點:
DECLARE @a INT
SET @a = 1
WHILE @a <= 10
BEGIN
PRINT @a
SET @a += 1
END
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
要在三角格式
;with cte
as
(
select 1 [sequence]
union all
select [sequence]+1 from cte where [sequence]<100
)
select * from cte
嘗試打印數量,通過使用普通型表達,我們可以做到這一點.....它的工作
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,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
- 1. 打印連續號碼
- 2. printf的連續打印
- 3. URL的連續編號
- 4. 連續自動編號
- 5. 怎麼算連續編號
- 6. data.frame段的連續編號
- 7. 連續參考編號
- 8. 在預設位置打印連續號碼
- 9. 打印唯一編號
- 10. JavaScript打印陣列編號
- 11. 打印編號是甚至
- 12. 用熱敏打印機打印連續紙的水晶報告
- 13. PHP打印到連續紙張打印機
- 14. 連續打印標籤問題(斑馬打印機)
- 15. 強制SAS連續打印結果
- 16. For循環打印連續數字?
- 17. 打印在單行與連續元素
- 18. 打印一個連續的BufferedImage
- 19. 在網頁上連續打印
- 20. Laravel:曲目編號OS連續登錄
- 21. 通過分節符連續頁編號
- 22. 基本Python-通過連續編號
- 23. 更新連續編號的SQL
- 24. SQL - SQLite的計數連續編號
- 25. 使IR變量編號連續
- 26. 連續獲取2個唯一編號?
- 27. 訪問編號爲連續的
- 28. 非連續編號的數組.NET
- 29. 用連續編號填充SQL列
- 30. 使用連續編號填充空表
Ummmm ..我懷疑它是否可以完成。反正你的問題+1。 – 2011-03-29 15:08:23
連續數字從什麼到什麼。你是什麼意思'打印'使用一個單一的查詢。 'PRINT'接受一個變量而不是查詢。 – 2011-03-29 15:14:56
爲什麼地球上有這個問題得到3 upvotes? '打印'1,2,3 ......''符合目前的要求。 – 2011-03-29 16:16:18