I'm coming to Oracle 11g from SQL Server, and I'm trying to use SQL Developer to create a simple function (getIDs) that will return the results of a query (SELECT id FROM employee). Nothing that I've found on the internet seems to work. Here is the code that I'm trying:
test_pkg:
CREATE OR REPLACE PACKAGE test_pkg AS
TYPE number_table IS TABLE OF NUMBER;
FUNCTION getIDs RETURN number_table;
END TEST_PKG;
test_pkg body:
CREATE OR REPLACE PACKAGE BODY test_pkg AS
FUNCTION getIDs RETURN number_table AS
ids number_table;
BEGIN
SELECT id BULK COLLECT INTO ids
FROM employee;
RETURN ids;
END getIDs;
END test_pkg;
SQL Developer Worksheet:
SELECT *
FROM TABLE(test_pkg.getIDs());
-- The above gives
-- ORA-00902: invalid datatype
DECLARE
nums test_pkg.number_table;
BEGIN
SELECT column_value BULK COLLECT INTO nums
FROM TABLE(test_pkg.getIDs);
END;
-- The above gives:
-- PLS-00642: local collection types not allowed in SQL statements
-- PL/SQL: ORA-22905: cannot access rows from a non-nested table item
If anyone could see what I'm doing wrong here, I'd greatly appreciate it. I've spent a lot of time searching for the answer and it seems like this should work. In the end I'll also want to query the results of the getIDs() function inside other functions, procedures, and queries.