How to Execute stored procedure from SQL Plus? - sql

I have a stored procedure in oracle and want to test it from SQLPlus.
If I use
execute my_stored_proc (-1,2,0.01)
I get this error
PLS-00306: wrong number or types of arguments in call to my_stored_proc
The beginning for the proc is this
create or replace PROCEDURE my_stored_proc
( a IN NUMBER,
b IN NUMBER,
c IN NUMBER,
z out NUMBER
) AS ....
Do I need to provide the a var for the out parameter, is so how? I tried:
var z NUMBER;
But get this error when I try to run the proc
execute my_stored_proc (-1,2,0.01,z)
PLS-00201: identifier 'Z' must be declared
Also when I was in SQL-Developer it gave me the usage and it show the inputs in reverse order, that is:
execute my_stored_proc(z number,c number,b number,a number);
Do you provide them in reverse order or is that just something with SQL-Developer
I did not write the procedure and I don't normally deal with them so I could be missing something obvious.
Thanks

You have two options, a PL/SQL block or SQL*Plus bind variables:
var z number
execute my_stored_proc (-1,2,0.01,:z)
print z

You forgot to put z as an bind variable.
The following EXECUTE command runs a PL/SQL statement that references a stored procedure:
SQL> EXECUTE -
> :Z := EMP_SALE.HIRE('JACK','MANAGER','JONES',2990,'SALES')
Note that the value returned by the stored procedure is being return into :Z

Related

PL/SQL Developer - Creating dynamic SQL

I'm using PL/SQL Developer. I'm trying to get query results to excel via vba. Since query is so long, i decided to create table with the query results and then simply get the table results with vba. In order to create table via excel i needed to create procedure with dynamic sql. So this is what i tried so far (even this simple example doesn't work):
create or replace procedure d_x IS
str VARCHAR(81) = 'create table as select 1 as x from dual'
BEGIN
EXECUTE IMMEDIATE str;
END;
Procedure completes without error. But when i try to execute it to create table it throws an error.
Execute statement:
EXECUTE d_x;
The execute statement throws 'ORA-00900' Invalid sql statement error.
I'm kinda new to pl sql so i couldn't find a solution to this.
Any help would be appreciated, thanks.
Procedure you posted can't possibly execute without errors because it is invalid. When fixed, looks like this:
SQL> create or replace procedure d_x IS
2 str VARCHAR(81) := 'create table test as select 1 as x from dual';
3 BEGIN
4 EXECUTE IMMEDIATE str;
5 END;
6 /
Procedure created.
In tools that support execute, you can run it as:
SQL> execute d_x
PL/SQL procedure successfully completed.
SQL> select * from test;
X
----------
1
"Correct" way - which works anywhere - is to enclose it (the procedure) into begin-end block:
SQL> drop table test;
Table dropped.
SQL> begin
2 d_x;
3 end;
4 /
PL/SQL procedure successfully completed.
SQL>
I suggest you do that.
In PL/SQL Developer you can click on the procedure name and choose 'Test', which will generate a calling script as a Test window.
You can only use execute in a Command window, because it's a SQL*Plus command and not part of SQL or PL/SQL, and the Command window is a SQL*Plus emulator.
In a SQL window you could either use a complete PL/SQL block:
begin
d_x;
end
/
or use the SQL call command:
call d_x();
Note that call() requires brackets regardless of whether or not the procedure has any parameters.
The / character is useful as a separator when using PLSQL blocks in a SQL window where you have more than one statement, otherwise PL/SQ Developer won't know where one ends and the next starts.
A Test window can only have one statement and only a single PL/SQL block or SQL statement is allowed, so there is no / character at the end.

Stored procedure in oracle sql

I'm trying to do a very simple stored procedure for a class project. I just want to use it to execute a simple insert statement.
What I have is:
CREATE OR REPLACE PROCEDURE insertSpa (p_spaID IN INTEGER, s_sdate IN VARCHAR2, p_address IN VARCHAR2)
AS
BEGIN
INSERT INTO spa( spaID,sdate,address)
VALUES (p_spaid, p_sdate, p_address);
END;
/
And then to call it, I've been using:
exec insertSpa( p_spaID => 9999, p_sDate => '12122012', p_address => '1234 Main St. 12345' );
When I try and run the first portion, I get an error that says "Procedure created with compilation errors." Here is what the spa relation is:
SPAID : NOT NULL NUMBER(38)
SDATE : VARCHAR2(8)
ADDRESS : VARCHAR2(100)
It appears that, among other things, you are trying to use SQL Server's T-SQL syntax in Oracle rather than PL/SQL. I'm guessing that spaid is intended to be an integer, sdate is intended to be an integer (though since it appears to represent a date, it really ought to be defined as a date), and address is intended to be a string. I'm also guessing that the spa table has three columns spaID, sDate, and address. If those guesses are accurate, something like
CREATE OR REPLACE PROCEDURE insertSpa( p_spaID IN INTEGER,
p_sdate IN INTEGER,
p_address IN VARCHAR2 )
AS
BEGIN
INSERT INTO spa( spaID, sDate, address )
VALUES( p_spaID, p_sdate, p_address );
END;
should compile. You would then call it
SQL> exec insertSpa( p_spaID => 9999, -
p_sDate => 12122012, -
p_address => '1234 Main St. 12345' );
The error that you are getting seems to imply that you never created the stored procedure. Did you actually run the CREATE PROCEDURE statement? It sounds like maybe you just created the script on your local client machine but then never executed it to create the procedure. You can run the CREATE PROCEDURE statement directly in SQL*Plus. Or you can save it to a file and execute the script in SQL*Plus
SQL> #<<name of .sql script>>
Presumably, from what you've shown, the procedure compiles OK, and it's the exec - which is just shorthand for an anonymous block - that says there is an error. It would have been helpful if you'd shown the error stack. (If the procedure, or any named blocked, said it had a compilation error, you could get the details with show errors, or by selecting from the user_errors view).
You created your procedure with the second parameter named s_sdate:
CREATE OR REPLACE PROCEDURE insertSpa (p_spaID IN INTEGER,
s_sdate IN VARCHAR2, p_address IN VARCHAR2)
... but when you call it you're saying the name is p_sDate:
exec insertSpa( p_spaID => 9999, p_sDate => '12122012', ...
The names have to match. Given the other names, presumably the procedure declaration should change to p_sDate. Actually, you have a mismatch between the procedure declaration and where you're using it in the procedure, so you'd get both errors - on compile and on exec - so you should definitely change it in the declaration. I missed that you'd said you had a procedure compilation, somehow.

Variable must be declared error in a stored procedure

I have a package whose body consists of a stored procedure.
The stored procedure makes a call to a stored procedure in another package. However I need to pass the result of a select statement as a parameter. so I stored the result in a variable and passed it as a parameter. However when the procedure call is performed, it says, the variable is not declared.
This is the sample code.
create or replace
PACKAGE BODY PKG_BODY
PROCEDURE PROC (error OUT VARCHAR2,
RESULT OUT BOOLEAN ) AS
-----------------------------
-----------------------------
DECLARE
PARAM TABLE1.COLUMN1%TYPE;
BEGIN
SELECT COLUMN1
INTO PARAM
FROM TABLE1
WHERE COLUMN2=ID AND COLUMN3='NAME';
END;
PUBLISH_MSG.PUBLISH_PMM_MSG('ID',PARAM,PO_ERR_MSG); --another procedure call
END PROC;
I am getting the following error :
PLS:00201 'Identifier PARAM must be declaed'
Do I have to declare the PARAM before somewhere else? Please help !!
You have defined that variable in a nested PL/SQL block - it can't be accessed from an outer block (it doesn't even exist after the block is finished). You should move definition of your param variable to the outer block, for example:
PROCEDURE PROC (error OUT VARCHAR2,
RESULT OUT BOOLEAN )
AS
PARAM TABLE1.COLUMN1%TYPE;
BEGIN
-----------------------------
-----------------------------
SELECT COLUMN1
INTO PARAM
FROM TABLE1
WHERE COLUMN2=ID AND COLUMN3='NAME';
PUBLISH_MSG.PUBLISH_PMM_MSG('ID',PARAM,PO_ERR_MSG); --another procedure call
END PROC;

Dynamic SQL not working as expected

create or replace procedure createtables
Authid current_user as
begin
execute immediate 'create table newcustomer as select * from customer';
end;
create or replace procedure e
is
begin
createtables;
select * from newcustomer;
end;
I got two procedures above. first one will create a new tables called newcustomer, second procedure will call the first procedure and query to the newcustomer table. when I try to compile this code, it says the table is not yet created, I don't really get it as I have called createtables procedure so I assume I have created the table.
Any help will be appreciated. Thanks
Compiling the second procedure without executing the first procedure first will fail, since the table has not been created.
You cannot compile a procedure that relies on objects that do not exist.
Use EXEC createtables before creating procedure e, and do not call createtables in there.
Procedure e will also not compile because you are not using the results of select * from newcustomer as cursor or store the results into variables.
EDIT:
Instead of procedures, you could use an anonymous block. Put the following into a file and execute it (via SQL*Plus for example):
Create Table newcustomer As Select * From customer;
Begin
Null; --# Do something with your new table in here.
End;
/

What is the difference between function and procedure in PL/SQL?

What is the difference between function and procedure in PL/SQL ?
A procedure does not have a return value, whereas a function has.
Example:
CREATE OR REPLACE PROCEDURE my_proc
(p_name IN VARCHAR2 := 'John') as begin ... end
CREATE OR REPLACE FUNCTION my_func
(p_name IN VARCHAR2 := 'John') return varchar2 as begin ... end
Notice how the function has a return clause between the parameter list and the "as" keyword. This means that it is expected to have the last statement inside the body of the function read something like:
return(my_varchar2_local_variable);
Where my_varchar2_local_variable is some varchar2 that should be returned by that function.
A function can be in-lined into a SQL statement, e.g.
select foo
,fn_bar (foo)
from foobar
Which cannot be done with a stored procedure. The architecture of the query optimiser limits what can be done with functions in this context, requiring that they are pure (i.e. the same inputs always produce the same output). This restricts what can be done in the function, but allows it to be used in-line in the query if it is defined to be "pure".
Otherwise, a function (not necessarily deterministic) can return a variable or a result set. In the case of a function returning a result set, you can join it against some other selection in a query. However, you cannot use a non-deterministic function like this in a correlated subquery as the optimiser cannot predict what sort of result set will be returned (this is computationally intractable, like the halting problem).
In dead simple way it makes this meaning.
Functions :
These subprograms return a single value; mainly used to compute and return a value.
Procedure :
These subprograms do not return a value directly; mainly used to perform an action.
Example Program:
CREATE OR REPLACE PROCEDURE greetings
BEGIN
dbms_output.put_line('Hello World!');
END ;
/
Executing a Standalone Procedure :
A standalone procedure can be called in two ways:
• Using the EXECUTE keyword
• Calling the name of procedure from a PL/SQL block
The procedure can also be called from another PL/SQL block:
BEGIN
greetings;
END;
/
Function:
CREATE OR REPLACE FUNCTION totalEmployees
RETURN number IS
total number(3) := 0;
BEGIN
SELECT count(*) into total
FROM employees;
RETURN total;
END;
/
Following program calls the function totalCustomers from an another block
DECLARE
c number(3);
BEGIN
c := totalEmployees();
dbms_output.put_line('Total no. of Employees: ' || c);
END;
/
Both stored procedures and functions are named blocks that reside in the database and can be executed as and when required.
The major differences are:
A stored procedure can optionally return values using out parameters, but can also be written in a manner without returning a value. But, a function must return a value.
A stored procedure cannot be used in a SELECT statement whereas a function can be used in a SELECT statement.
Practically speaking, I would go for a stored procedure for a specific group of requirements and a function for a common requirement that could be shared across multiple scenarios. For example: comparing between two strings, or trimming them or taking the last portion, if we have a function for that, we could globally use it for any application that we have.
The following are the major differences between procedure and function,
Procedure is named PL/SQL block which performs one or more tasks. where function is named PL/SQL block which performs a specific action.
Procedure may or may not return value where as function should return one value.
we can call functions in select statement where as procedure we cant.
In the few words - function returns something. You can use function in SQL query.
Procedure is part of code to do something with data but you can not invoke procedure from query, you have to run it in PL/SQL block.
we can call a stored procedure inside stored Procedure,Function within function ,StoredProcedure within function but we can not call function within stored procedure.
we can call function inside select statement.
We can return value from function without passing output parameter as a parameter to the stored procedure.
This is what the difference i found. Please let me know if any .