# SQL来生成一个从1到100的数字列表

### 10 Solutions collect form web for “SQL来生成一个从1到100的数字列表”

` `Select Rownum r From dual Connect By Rownum <= 100` `

ORACLE PL / SQL中另一个有趣的解决scheme：

` ` SELECT LEVEL n FROM DUAL CONNECT BY LEVEL <= 100;` `

` `SELECT V FROM ( SELECT 1 V FROM DUAL ) T MODEL DIMENSION BY (ROWNUM R) MEASURES (V) RULES ITERATE (100) ( V[ITERATION_NUMBER] = CV(R) + 1 ) ORDER BY 1` `

certificate： http : //sqlfiddle.com/#!4/ d41d8/ 20837

` `with bnd as (select 4 lo, 9 hi from dual) select (select lo from bnd) - 1 + level r from dual connect by level <= (select hi-lo from bnd);` `

` `4 5 6 7 8` `

` `SELECT ROWNUM FROM (SELECT 1 AS c FROM dual GROUP BY CUBE(1,1,1,1,1,1,1) ) sub WHERE ROWNUM <=100;` `

Rextester演示

` `with digits as ( select mod(rownum,10) as num from dual connect by rownum <= 10 ) select a.num*10+b.num as num from digits a ,digits b order by num ;` `

` `with range as ( select mod(rownum,100) as num from dual connect by rownum <= 100 ), AllPossible as ( select a.num*100+b.num as TicketNum from batches a ,range b order by num ) select TicketNum as TicketsSold from AllPossible where AllPossible.Ticket not in (select TicketNum from TicketsReturned) ;` `

…为了说明为什么这样的事情会有用

` `CREATE OR REPLACE FUNCTION [schema].FN_TABLE_NUMBERS( NUMINI INTEGER, NUMFIN INTEGER, EXPONENCIAL INTEGER DEFAULT 0 ) RETURN TBL_NUMBERS IS NUMEROS TBL_NUMBERS; INDICE NUMBER; BEGIN NUMEROS := TBL_NUMBERS(); FOR I IN ( WITH TABLA AS (SELECT NUMINI, NUMFIN FROM DUAL) SELECT NUMINI NUM FROM TABLA UNION ALL SELECT (SELECT NUMINI FROM TABLA) + (LEVEL*TO_NUMBER('1E'||TO_CHAR(EXPONENCIAL))) NUM FROM DUAL CONNECT BY (LEVEL*TO_NUMBER('1E'||TO_CHAR(EXPONENCIAL))) <= (SELECT NUMFIN-NUMINI FROM TABLA) ) LOOP NUMEROS.EXTEND; INDICE := NUMEROS.COUNT; NUMEROS(INDICE):= i.NUM; END LOOP; RETURN NUMEROS; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN NUMEROS; WHEN OTHERS THEN RETURN NUMEROS; END; /` `

` `CREATE OR REPLACE TYPE [schema]."TBL_NUMBERS" IS TABLE OF NUMBER; /` `

` `SELECT COLUMN_VALUE NUM FROM TABLE([schema].FN_TABLE_NUMBERS(1,10))--integers difference: 1;2;.......;10` `

` `SELECT COLUMN_VALUE NUM FROM TABLE([schema].FN_TABLE_NUMBERS(1,10,-1));--with 0.1 difference: 1;1.1;1.2;.......;10 SELECT COLUMN_VALUE NUM FROM TABLE([schema].FN_TABLE_NUMBERS(1,10,-2));--with 0.01 difference: 1;1.01;1.02;.......;10` `

` `select 1 from dual union select 2 from dual union select 3 from dual union select 4 from dual union select 5 from dual union select 6 from dual union select 7 from dual union select 8 from dual union select 9 from dual union select 10 from dual union select 11 from dual union select 12 from dual union select 13 from dual union select 14 from dual union select 15 from dual union select 16 from dual union select 17 from dual union select 18 from dual union select 19 from dual union select 20 from dual union select 21 from dual union select 22 from dual union select 23 from dual union select 24 from dual union select 25 from dual union select 26 from dual union select 27 from dual union select 28 from dual union select 29 from dual union select 30 from dual union select 31 from dual union select 32 from dual union select 33 from dual union select 34 from dual union select 35 from dual union select 36 from dual union select 37 from dual union select 38 from dual union select 39 from dual union select 40 from dual union select 41 from dual union select 42 from dual union select 43 from dual union select 44 from dual union select 45 from dual union select 46 from dual union select 47 from dual union select 48 from dual union select 49 from dual union select 50 from dual union select 51 from dual union select 52 from dual union select 53 from dual union select 54 from dual union select 55 from dual union select 56 from dual union select 57 from dual union select 58 from dual union select 59 from dual union select 60 from dual union select 61 from dual union select 62 from dual union select 63 from dual union select 64 from dual union select 65 from dual union select 66 from dual union select 67 from dual union select 68 from dual union select 69 from dual union select 70 from dual union select 71 from dual union select 72 from dual union select 73 from dual union select 74 from dual union select 75 from dual union select 76 from dual union select 77 from dual union select 78 from dual union select 79 from dual union select 80 from dual union select 81 from dual union select 82 from dual union select 83 from dual union select 84 from dual union select 85 from dual union select 86 from dual union select 87 from dual union select 88 from dual union select 89 from dual union select 90 from dual union select 91 from dual union select 92 from dual union select 93 from dual union select 94 from dual union select 95 from dual union select 96 from dual union select 97 from dual union select 98 from dual union select 99 from dual union select 100 from dual;` `
` `SELECT * FROM `DUAL` WHERE id>0 AND id<101` `

• 如何与工会订购
• SQL SERVER：获取两个date之间的总天数
• 最有效的T-SQL方法将左侧的varchar填充到一定的长度？
• 为什么SQL查询的结果不按照我预期的顺序回来？
• SQL Server 2008pipe理工作室没有检查我的查询的语法
• 如何使用“as”关键字在Oracle中为表格添加别名？
• 用SQLite完全外联接
• 为什么MySQL在FULL OUTER JOIN上报告语法错误？
• SQL ORDER BY按特定顺序的多个值？
• 使用LIKE和IN条件的参数化查询
• 整理是什么意思？