I am looking to create a simple store procedureto return a list of all user names in a table called dba_users.
The select I am using is:
SELECT username FROM dba_users
When I create a PROCEDURE with the following syntax it is created, but fails to execute:
CREATE OR REPLACE PROCEDURE user_list_display
IS
BEGIN
SELECT username FROM dba_users
END;
For this I get
ORA-00900: invalid SQL statement:
EXECUTE user_list_display;
you had better using an implicit cursor
with dbms_output.put_line
:
SQL> set serveroutput on;
SQL> CREATE OR REPLACE PROCEDURE user_list_display IS
BEGIN
FOR c in ( SELECT username FROM dba_users )
LOOP
dbms_output.put_line(c.username);
END LOOP;
END;
/
SQL> exec user_list_display;
In Oracle12c, You can use DBMS_SQL.RETURN_RESULT
.
CREATE OR REPLACE PROCEDURE user_list_display
IS
v_cursor SYS_REFCURSOR;
BEGIN
OPEN v_cursor FOR SELECT username FROM dba_users;
DBMS_SQL.RETURN_RESULT ( v_cursor );
END;
/
Then execute it as
EXEC user_list_display;
In lower versions,from SQL* Plus (or execute as script in SQL Developer ), you may use a REFCURSOR
OUT
variable and PRINT
to display the result.
CREATE OR REPLACE PROCEDURE user_list_display( output OUT SYS_REFCURSOR )
IS
BEGIN
OPEN output FOR SELECT username FROM dba_users;
END;
/
You may run these 3 lines whenever you want to see the output.
VARIABLE output REFCURSOR
EXEC user_list_display(:output)
PRINT output
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.