Oracle.如何使用SQL获取模拟数据?

fhg3lkii  于 2023-10-16  发布在  Oracle
关注(0)|答案(1)|浏览(128)

我需要使用SQL获取虚拟数据-10行和10列或100 x100或其他任意数字。
我想这样做:

select
        (select * from
                (
                    select     level lvl
                    from       dual
                    connect by level <= 10
                )
            pivot (
                max(lvl)
                for lvl in (1,2,3,4,5,6,7,8,9,10)
            )
        )
    from dual
    connect by level <= 10

但这段代码会抛出一个错误
有没有可能用一个SQL查询来实现这一点?
如果没有,最好的方法是什么?

u0sqgete

u0sqgete1#

您当前的结构是:

select ( select < 10 columns > ... )
from dual
connect by level <= 10

Oracle期望子查询中有一个列,而不是10(或100)。
你不需要一个子查询,你可以做:

select * from
(
    select     level lvl
    from       dual
    connect by level <= 10
)
pivot (
    max(lvl)
    for lvl in (1,2,3,4,5,6,7,8,9,10)
)
connect by level <= 10

| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| --|--|--|--|--|--|--|--|--|--|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
fiddle

相关问题