l made oracle procedure in dbeaver like this
CREATE OR REPLACE PROCEDURE p_test
IS
I_MESSAGE VARCHAR2(100) := 'test';
BEGIN
dbms_output.put_line(I_MESSAGE);
END;
then l run exec p_test;
procedure was not working with ORA-00900 error
l don't understand why not working..
exec p_test is an sql*plus command.
If you are using an IDE you should try:
BEGIN
p_test;
END;
Using
BEGIN
p_test;
END;
It's work.
EXEC is SQL*Plus command, it is no a part of PL/SQL.
Related
I expected the code like:
create or replace procedure dmp(t in varchar2)
AS
BEGIN
EXECUTE IMMEDIATE 'SELECT * FROM ' || t;
END;
/
BEGIN
dmp('SOMETABLE');
END;
to be the same as SELECT * FROM SOMETABLE. However, calling the stored procedure does not actually output anything -- for any table, including the obviously non-empty ones... Why is that? How would I write a stored procedure, that would output result(s) of queries inside it?
Assuming that you are using a client like SQL*Plus or one that supports a subset of SQL*Plus commands like SQL Developer, you can do something like this (note that I am ignoring the potential for SQL injection attacks).
variable rc refcursor;
/
create or replace procedure get_cursor( p_tableName in varchar2,
p_rc out sys_refcursor )
as
begin
open p_rc for 'select * from ' || p_tableName;
end;
/
begin
get_cursor( 'dual', :rc );
end;
/
print rc;
l made oracle procedure in dbeaver like this
CREATE OR REPLACE PROCEDURE p_test
IS
I_MESSAGE VARCHAR2(100) := 'test';
BEGIN
dbms_output.put_line(I_MESSAGE);
END;
then l run exec p_test;
procedure was not working with ORA-00900 error
l don't understand why not working..
exec p_test is an sql*plus command.
If you are using an IDE you should try:
BEGIN
p_test;
END;
Using
BEGIN
p_test;
END;
It's work.
EXEC is SQL*Plus command, it is no a part of PL/SQL.
Is it possible, if yes, what would the syntax look like for a stored proc which would pick a stored procedure from a list in a table and then executes it in an oracle db?
The stored procedure should use EXECUTE IMMEDIATE to execute an anonymous PL/SQL block with the procedure name. This can be as simple as creating a string like begin proc_name; end;. Things get more difficult if there are parameters and return values.
create table proc_table(id number, procedure_name varchar2(100));
insert into proc_table values(1, 'proc1');
insert into proc_table values(2, 'proc2');
create or replace procedure proc1 is begin dbms_output.put_line('1'); end;
/
create or replace procedure proc2 is begin dbms_output.put_line('2'); end;
/
begin
for procedures in
(
select procedure_name
from proc_table
order by procedure_name
) loop
execute immediate 'begin '||procedures.procedure_name||'; end;';
end loop;
end;
/
Output:
1
2
create or replace procedure get_emp(p_deptno number,emp_det out sys_refcursor)
is
begin
open emp_det for select * from emp where deptno=p_deptno;
end;
/
how to pass parameters for sys_refcursor and how to print that result using anonymous blocks in oracle pl/sql
Just declare a REFCURSOR variable and pass it to ur procedure
You can open
DECLARE
MYCUR SYS_REFCURSOR;
BEGIN
get_emp(123,MYCUR);
FOR I in MYCUR
LOOP
-- ur statements
END LOOP;
END;
/
Also like this using SQLPLUS. Output would be like you run a select query, with what ever sent in the cursor.
VARIABLE MYCUR REFCURSOR;
EXECUTE get_emp(:MYCUR);
print MYCUR;
I am not experienced with db (actually not at all) and I face a problem:
I have oracle 11g and I am using PL/SQL developer.
I have a simple procedure :
type t_ref_cursor is ref cursor;
procedure fakeProc (
io_cursor in out t_ref_cursor
)
is
begin
open io_cursor for
SELECT * from myTable;
end fakeProc;
Now I want to run it as a SQL window (not in a test window)
What I am trying to run:
v_cur cursor;
begin
fakeProc(:v_cur);
end;
I get errors:
ORA-00900:Invalid SQL statement
ORA-01008:not all variables bound
So can you point me the right way to run a procedure like this(with begin -end)?
Use something like this :
declare v_cur SYS_REFCURSOR;
begin
fakeProc(v_cur);
end;
And the procedure looks like:
CREATE OR REPLACE PROCEDURE FAKEPROC(
io_cursor in out SYS_REFCURSOR
)
IS
begin
open io_cursor for
SELECT * from resource_map;
END FAKEPROC;
Don't forget to close cursor after finishing working with it.
Version 7.1.4 of PL/SQL Developer doesn't support ref cursor:
SQL> VARIABLE p_cur REFCURSOR;
REFCURSOR not supported
Later versions may support them (in a command window), or you can use SQL*Plus. This is a direct copy-paste from SQL*Plus:
SQL> CREATE OR REPLACE PROCEDURE prc (p_cur OUT SYS_REFCURSOR) IS
2 BEGIN
3 OPEN p_cur FOR SELECT * FROM dual;
4 END;
5 /
Procedure created.
SQL> -- declare variable
SQL> VARIABLE p_cur REFCURSOR;
SQL> BEGIN
2 prc(:p_cur);
3 END;
4 /
PL/SQL procedure successfully completed.
SQL> print p_cur
DUM
---
X