I have a function that returns a SYS_REFCURSOR that has a single row but multiple columns. What I'm looking to do is to be able to have a SQL query that 开发者_如何学JAVAhas nested sub-queries using the column values returned in the SYS_REFCURSOR. Alternative ideas such as types, etc would be appreciated. Code below is me writing on-the-fly and hasn't been validated for syntax.
--Oracle function
CREATE DummyFunction(dummyValue AS NUMBER) RETURN SYS_REFCURSOR
IS
RETURN_DATA SYS_REFCURSOR;
BEGIN
OPEN RETURN_DATA
SELECT
TO_CHAR(dummyValue) || 'A' AS ColumnA
,TO_CHAR(dummyValue) || 'B' AS ColumnB
FROM
DUAL;
RETURN RETURN_DATA;
END;
--sample query with sub-queries; does not work
SELECT
SELECT ColumnA FROM DummyFunction(1) FROM DUAL AS ColumnA
,SELECT ColumnB FROM DummyFunction(1) FROM DUAL AS ColumnB
FROM
DUAL;
A SYS_REFCURSOR won't work for the intended use - you need to create an Oracle TYPE:
CREATE TYPE your_type IS OBJECT (
ColumnA VARCHAR2(100),
ColumnB VARCHAR2(100)
)
Update your function:
CREATE DummyFunction(dummyValue AS NUMBER)
RETURN your_type
IS
BEGIN
INSERT INTO your_type
SELECT TO_CHAR(dummyValue) || 'A' AS ColumnA,
TO_CHAR(dummyValue) || 'B' AS ColumnB
FROM DUAL;
RETURN your_type;
END;
Then you can use:
SELECT (SELECT ColumnA FROM table(DummyFunction(1))) AS ColumnA,
(SELECT ColumnB FROM table(DummyFunction(1))) AS ColumnB
FROM DUAL
The example is overcomplicated - all you need to use is:
SELECT x.columna,
x.columnb
FROM table(DummyFunction(1)) x
精彩评论