日期:2014-05-17 浏览次数:20606 次
create table tb(ID int,BH int,RS int) insert into tb select 1,13,12 union select 2,13,20 union select 3,13,5 select a.*,JG=row_number() over(order by id) from tb a,master..spt_values where type='P' and a.RS>number /* ID BH RS JG ----------- ----------- ----------- ------- 1 13 12 1 1 13 12 2 1 13 12 3 1 13 12 4 1 13 12 5 .......
------解决方案--------------------
--> 测试数据:[tb] IF OBJECT_ID('[tb]') IS NOT NULL DROP TABLE [tb] GO CREATE TABLE [tb]([ID] INT,[BH] INT,[RS] INT) INSERT [tb] SELECT 1,13,12 UNION ALL SELECT 2,13,20 UNION ALL SELECT 3,13,5 --------------开始查询-------------------------- SELECT a.*,ROW_NUMBER() OVER(ORDER BY GETDATE()) FROM [tb] AS a OUTER APPLY ( SELECT number FROM master..spt_values WHERE type='p' AND number< [RS] ) AS opit ----------------结果---------------------------- /* ID BH RS (无列名) 1 13 12 1 1 13 12 2 1 13 12 3 1 13 12 4 1 13 12 5 1 13 12 6 1 13 12 7 1 13 12 8 1 13 12 9 1 13 12 10 1 13 12 11 1 13 12 12 2 13 20 13 2 13 20 14 2 13 20 15 2 13 20 16 2 13 20 17 2 13 20 18 2 13 20 19 2 13 20 20 2 13 20 21 2 13 20 22 2 13 20 23 2 13 20 24 2 13 20 25 2 13 20 26 2 13 20 27 2 13 20 28 2 13 20 29 2 13 20 30 2 13 20 31 2 13 20 32 3 13 5 33 3 13 5 34 3 13 5 35 3 13 5 36 3 13 5 37 */