Create stored procedure error in oracle - sql

I create a extremely basic stored procedure but oracle toad always shows "Warning: compiled but with compilation errors".
Here is the code:
CREATE OR REPLACE PROCEDURE update_history (
tab1_name IN VARCHAR2
)
AS
BEGIN
END;

Oracle does not handle the empty block; so you can simply add a NULL statement:
CREATE OR REPLACE PROCEDURE update_history (
tab1_name IN VARCHAR2
)
AS
BEGIN
null;
END;

Related

PL/SQL OUT SYS_REFCURSOR on associative array

I have an associative array inside a procedure:
create or replace PROCEDURE RET_STRING_TAB(o_cursor OUT SYS_REFCURSOR) IS
type type_ntz is record(type_id number, type_uname varchar(32), type_pwd varchar(16));
type ntz_array is table of type_ntz index by pls_Integer;
v_ntz_array ntz_array;
BEGIN
Select id,uname,pwd BULK COLLECT INTO v_ntz_array FROM users;
As you can see i want to out a refcusor on that array, to work further in Java with it. I tried doing:
OPEN o_cursor FOR
SELECT * FROM TABLE(v_ntz_array);
But it keeps saying:
PL/SQL: SQL Statement ignored
PL/SQL: cannot access rows from a non-nested table item
PLS: expression is of wrong type
Thanks in advance for any help!
You TYPE needs to be create out of the procedure, so your calling program (ie: java) can access it and know it's composition:
CREATE OR REPLACE TYPE type_ntz IS OBJECT(type_id number, type_uname varchar(32), type_pwd varchar(16));
/
then:
create or replace PROCEDURE RET_STRING_TAB(o_cursor OUT SYS_REFCURSOR) IS [...]
UPDATE
I've re-read your question and for me the easiest way to do what you are looking for is:
create or replace PROCEDURE RET_STRING_TAB(o_cursor OUT SYS_REFCURSOR) IS
BEGIN
OPEN o_cursor FOR
Select id,uname,pwd FROM users;
END;
/

IF statement using procedure parameters?

I’m wondering if it’s technically possible to use procedure parameters in IF statement or how to solve this problem in different manner.
Situation description:
I have a procedure within other procedure where arguments of inner procedure are parameters of outer procedure. I would
like to use parameters of inner procedure within it’s body in IF statement eg: IF parameter1 OR parameter2 OR parameter3 IS NOT NULL THEN. . It’s worth to be mentioned that all parameters of outer procedure have to be DEFAULT NULL. Any ideas? Cheers.
are you trying to do like this :
--first procedure
CREATE OR REPLACE PROCEDURE p1(f_name varchar2 ,sal NUMBER )
IS
BEGIN
p2(f_name,sal);
END;
--second procedure
CREATE OR replace PROCEDURE p2(f_name varchar2 ,sal NUMBER )
IS
BEGIN
IF f_name IS NOT NULL OR sal IS NOT NULL
THEN
dbms_output.put_line(f_name ||' '||sal);
END IF;
END;
then call the first procedure :
BEGIN
p1( 'john',null );
END;

executing a stored proc with output parameter

I am using SQLDEVELOPER TO make a stored procedure. this is my procedure
create or replace PROCEDURE SPP
(
inpt IN VARCHAR2,
opt OUT VARCHAR2
)
AS
BEGIN
SELECT U_ID
INTO opt
FROM TABLE_NAME
WHERE U_ID=inpt;
END;
so it should return the value in opt which i can then print or whatever.
i dont know how to execute it i tried running this script
var opt VARCHAR2;
exec SPP('test_user') := opt
but it doesnt work this is my first time doing stored procs and I'm really confused any help is appreciated
how do i make it run? i can make a stored proc run with input variables but with an output variable i mess up and I cant do it without using the SELECT (item) INTO format.
In order to call the procedure, you'd need to pass in a variable that the procedure can populate. Something like
DECLARE
l_output table_name.u_id%type;
BEGIN
spp( 'test_user', l_output );
END;
Now, that being said, declaring a stored procedure whose only purpose is to query the database and return a value is generally the wrong architectural choice. That's why functions exist.
CREATE OR REPLACE FUNCTION some_function( p_input IN table_name.u_id%type )
RETURN table_name.u_id%type
IS
l_uid table_name.u_id%type;
BEGIN
SELECT u_id
INTO l_uid
FROM table_name
WHERE u_id = p_input;
RETURN l_uid;
END;
which you can then call either in a PL/SQL block
DECLARE
l_output table_name.u_id%type;
BEGIN
l_output := some_function( 'test_user' );
END;
or in a SQL statement
SELECT some_function( 'test_user' )
FROM dual;

error of running a stored proceudre in IBM netezza SQL database

I need to create a stored procedure in IBM netezza SQL database from IBM Aginity workbench.
This is my SQL code to create the SP:
CREATE OR REPLACE PROCEDURE "SP_drop_a_table_if_exists"(varchar(128))
RETURNS boolean
EXECUTE AS OWNER
LANGUAGE NZPLSQL AS
BEGIN_PROC
declare
oname alias for $1;
o record;
begin
select otype into o
from (
select 'TABLE' otype from _v_table where tablename = upper(oname)
union all
select 'VIEW' otype from _v_view where viewname = upper(oname)
) x;
if found then
execute immediate 'DROP '||o.otype||' '||oname;
end if;
end;
END_PROC;
I created successfully.
But, when I ran it,
CALL SP_drop_a_table_if_exists('test_a_table':: varchar(128))
I got error:
ERROR[42S02] error: function 'sp_drop_a_table_if_exists(varchar)' does not exists
unable to identify a function that satisdy the given argument types
you may need to add explicit typecasts
Any help would be appreciated !
You created your stored procedure with mixed case inside of double quotes...
CREATE OR REPLACE PROCEDURE "SP_drop_a_table_if_exists"(varchar(128))
...but when you call the stored procedure you don't use double quotes, so the name is being converted all upper case.
CALL SP_drop_a_table_if_exists('test_a_table':: varchar(128))
Try this instead:
CALL "SP_drop_a_table_if_exists"('test_a_table':: varchar(128))
I should also mention that more recent versions of NPS support this syntax for the DROP TABLE command:
DROP TABLE TABLENAME IF EXISTS;

stored PL/sql function using select statement

Example i have a stored pl/sql
CREATE OR REPLACE PROCEDURE number( test in NUMBER )
........
// rest of code
isn't possible that i don't want run this execute
execute number(2);
i want run with
select * from number(2);
isn't possible to run stored pl/sql script with select statement to call the function instead of execute?
You can't execute a PROCEDURE from SQL; however, you can execute a FUNCTION from SQL.
First, redefine NUMBER as a FUNCTION:
CREATE OR REPLACE FUNCTION NUMBER(pTest IN NUMBER) RETURN NUMBER IS
someValue NUMBER := pTest * 100;
BEGIN
-- whatever
RETURN someValue;
END;
Then execute it from a SELECT statement as
SELECT NUMBER(2) FROM DUAL;
Share and enjoy.