How to generate a list of number in SQL as it was a list of comprehension?

别等时光非礼了梦想. 提交于 2019-12-12 12:22:52

问题


In my case, using sql anywhere (sybase).

Something similar to haskell. [1..100].

I don't know how to generate a random simple list of 1 up to 100.

I only could do it doing:

select 1
union 
select 2
union
select 3

Google did not provide any sample, I suspect this feature does not exist.


回答1:


SQL Anywhere contains an sa_rowgenerator stored procedure, which can be used for this purpose. For example:

select row_num from sa_rowgenerator( 1, 100 )

returns a result set of 100 rows from 1 to 100 inclusive. A link to the documentation (for version 12.0.1) is here.

Disclaimer: I work for SAP/Sybase in the SQL Anywhere engineering.




回答2:


I find the easiest way for a short list is something like:

select t.*
from (select row_number() over (order by (select NULL)) as num
      from Information_Schema.columns
     ) t
where num <= 100

The columns table usually has at least 100 rows. Other tables can also be used.

For large numbers, something like the following:

with digits as (
      select 0 as dig 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
),
     numbers as
      select d1.dig*10+d2.dig
      from digits d1 cross join
           digits d2
)
 . . .



回答3:


Oracle queries - use any number to start/end:

 SELECT 99 + ROWNUM
   FROM dual       
 CONNECT BY ROWNUM <= 100 + 1
/

SELECT 99 + LEVEL
  FROM dual       
CONNECT BY LEVEL <= 100 + 1
/


来源:https://stackoverflow.com/questions/14339369/how-to-generate-a-list-of-number-in-sql-as-it-was-a-list-of-comprehension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!