我正在寻找替代方案CONNECT BY
:
Select SUBSTR(str,1,LEVEL) OUTPUT FROM
(
SELECT 'ORACLE' As str FROM DUAL
)
CONNECT BY LEVEL<=LENGTH(str);
有什么建议么?
我正在寻找替代方案CONNECT BY
:
Select SUBSTR(str,1,LEVEL) OUTPUT FROM
(
SELECT 'ORACLE' As str FROM DUAL
)
CONNECT BY LEVEL<=LENGTH(str);
有什么建议么?
几个选项:
查询 1 - 使用集合:
这些值在这里是硬编码的,所以这可能不是最好的解决方案,除非你有一个固定长度的列表。
SELECT SUBSTR( 'ORACLE', 1, COLUMN_VALUE ) AS value
FROM TABLE( SYS.ODCINUMBERLIST( 1, 2, 3, 4, 5, 6 ) )
结果:
| VALUE |
|--------|
| O |
| OR |
| ORA |
| ORAC |
| ORACL |
| ORACLE |
查询 2 - 使用流水线函数生成集合:
CREATE OR REPLACE FUNCTION get_numbers (
maximum IN NUMBER
) RETURN SYS.ODCINUMBERLIST PIPELINED AS
BEGIN
FOR i IN 1 .. maximum LOOP
PIPE ROW(i);
END LOOP;
RETURN;
END;
/
SELECT SUBSTR( 'ORACLE', 1, COLUMN_VALUE ) AS value
FROM TABLE( get_numbers( LENGTH( 'ORACLE' ) ) )
结果:
| VALUE |
|--------|
| O |
| OR |
| ORA |
| ORAC |
| ORACL |
| ORACLE |
查询 3 - 使用非流水线函数生成集合:
CREATE OR REPLACE FUNCTION get_numbers2 (
maximum IN NUMBER
) RETURN SYS.ODCINUMBERLIST
AS
v_nums SYS.ODCINUMBERLIST := SYS.ODCINUMBERLIST();
BEGIN
v_nums.EXTEND( maximum );
FOR i IN 1 .. maximum LOOP
v_nums(i) := i;
END LOOP;
RETURN v_nums;
END;
/
SELECT SUBSTR( 'ORACLE', 1, COLUMN_VALUE ) AS value
FROM TABLE( get_numbers2( LENGTH( 'ORACLE' ) ) )
结果:
| VALUE |
|--------|
| O |
| OR |
| ORA |
| ORAC |
| ORACL |
| ORACLE |
查询 4 - 使用递归子查询分解:
WITH data ( value ) AS (
SELECT 'ORACLE' FROM DUAL
UNION ALL
SELECT SUBSTR( value, 1, LENGTH( value ) - 1 )
FROM data
WHERE LENGTH( value ) > 1
)
SELECT * FROM data
结果:
| VALUE |
|--------|
| ORACLE |
| ORACL |
| ORAC |
| ORA |
| OR |
| O |
SELECT RPAD(LPAD('ORACLE',ROWNUM,'ORACLE'),7) AS GRAPH from emp where rownum <7
这是一个金字塔计划。
SELECT RPAD(LPAD('ILOVEYOU',ROWNUM,'ILOVEYOU'),9) ||LPAD(RPAD('ILOVEYOU',ROWNUM,'ILOVEYOU'),9)LOVEGRAPH FROM EMP WHERE ROWNUM<9 UNION ALL SELECT * FROM( SELECT RPAD(LPAD('ILOVEYOU',ROWNUM,'ILOVEYOU'),9) ||LPAD(RPAD('ILOVEYOU',ROWNUM,'ILOVEYOU'),9)LOVEGRAPH FROM EMP WHERE ROWNUM<9 ORDER BY ROWNUM DESC) /
II 伊洛伊伊洛伊洛伊洛夫伊洛夫伊洛夫伊洛夫伊洛夫伊洛伊洛伊洛伊洛伊伊洛伊伊洛伊伊洛伊伊洛伊伊洛伊伊洛伊伊洛伊伊洛伊伊洛伊伊洛伊伊洛伊伊洛伊伊洛伊伊洛伊伊洛伊伊洛伊伊洛伊伊洛伊伊洛伊伊洛伊伊洛
选择了 16 行