What can be the smallest Procedure? - oracle

I want to create Procedure/Function without Parameters.
create or replace procedure p_newname ()
iS
begin
dbms_output.put_line('ok');
end p_newname;
/
But, i am getting the following error message:
"Warning: Procedure created with compilation errors."
Actually i wanted to call this Procedure using - Standalone Execution but without any involvement of parameters be it Formal parameters or Actual parameters:
EXECUTE p_newname();
Expected result should be - ok

Because, you need to remove parentheses after procedure name if procedure doesn't have any parameter during the creation.
But,you can call either by
SQL> exec p_newname();
/
or
SQL> exec p_newname;
/
or
SQL> begin
p_newname;
end;
/
or
SQL> begin
p_newname();
end;
/
of course without forgetting to issue
SQL> set serveroutput on
before them the results to be able to be printed on the screen.

As the question is
What can be the smallest Procedure?
and if it means "use as few letters as possible", then something like this might be the answer:
SQL> create procedure p as begin null; end;
2 /
Procedure created.
SQL>

Related

Can we use Parameter mode directly inside BEGIN part?

CREATE OR REPLACE PROCEDURE do_something(p_a IN OUT VARCHAR2)
AS
BEGIN
p_a := 'something';
END;
/
Procedure created.
SQL> set serveroutput on
SQL> VARIABLE a VARCHAR2(30)
SQL> exec do_something(:a);
PL/SQL procedure successfully completed.
While am execute above procedure, it wont show output called something from procedure. What is the reason, what is happening backend?
The variable holds the value you've assigned in your session context. You can use SQL*Plus PRINT to see it:
SQL> PRINT :a;

How to call stored procedure with only OUT parameter?

I have created a stored procedure in Oracle 11g:
CREATE OR REPLACE PROCEDURE greetings(cnt OUT VARCHAR2)
AS
BEGIN
SELECT COUNT(*)
INTO cnt
FROM SYS.all_tables;
END greetings;
but I am unable to call it.
I have tried the following code snippets:
EXEC GREETINGS();
EXEC GREETINGS;
CALL GREETINGS;
The procedure requires one parameter, so - provide it.
SQL> CREATE OR REPLACE PROCEDURE greetings(cnt OUT VARCHAR2)
2 AS
3 BEGIN
4 SELECT COUNT(*)
5 INTO cnt
6 FROM SYS.all_tables;
7 END greetings;
8 /
Procedure created.
One option, which works everywhere, is to use an anonymous PL/SQL block:
SQL> set serveroutput on
SQL> declare
2 l_cnt number;
3 begin
4 greetings(l_cnt);
5 dbms_output.put_line(l_cnt);
6 end;
7 /
87
PL/SQL procedure successfully completed.
Another one works in SQL*Plus (or any other tool which is capable of simulating it):
SQL> var l_cnt number;
SQL> exec greetings(:l_cnt);
PL/SQL procedure successfully completed.
SQL> print l_cnt;
L_CNT
----------
87
Regarding the call example, this is explained in EXECUTE recognizes a stored procedure, CALL does not. It's not obvious from the syntax documentation but it does require brackets, so it is (rather unhelpfully) rejecting the whole thing and giving the impression that greetings is the problem, when actually it is not:
SQL> call greetings;
call greetings
*
ERROR at line 1:
ORA-06576: not a valid function or procedure name
while using the mandatory brackets gets you the real issue:
SQL> call greetings();
call greetings()
*
ERROR at line 1:
ORA-06553: PLS-306: wrong number or types of arguments in call to 'GREETINGS'
As others have pointed out, you are missing the parameter.
SQL> var n number
SQL>
SQL> call greetings(:n);
Call completed.
SQL> print :n
N
----------
134
execute is just a handy SQL*Plus shortcut for the PL/SQL block begin xxx; end; which is less fussy about brackets and gives the same error message with or without them.
(variable and print are SQL*Plus commands and may not be supported in other environments.)
There's no problem with the procedure body. You can call like this :
SQL> var nmr number;
SQL> exec greetings(:nmr);
PL/SQL procedure successfully completed
nmr
------------------------------------------
306 -- > <a numeric value returns in string format>
Oracle doesn't care assigning a numeric value to a string. The execution prints the result directly, but whenever you want you can recall that value of variable(nmr) again, and print as
SQL> print nmr
nmr
---------
306

when try to excure procedure it said compliation error in oracle?

I try to write select procedure in oracle.but it compile success, when I try to execute it given error.
set serveroutput on;
CREATE OR REPLACE PROCEDURE retrieve_decrypt(
custid in NUMBER,
column_name in VARCHAR2,
test_value OUT VARCHAR2
)
AS
BEGIN
-- enc_dec.decrypt(column_name,password) into test_value from employees where custid=5;
COMMIT;
END;
/
set serveroutput on;
EXEC retrieve_decrypt(5,'creditcardno');
the error says ,
This is your procedure:
SQL> create or replace procedure retrieve_decrypt
2 (custid in number,
3 column_name in varchar2,
4 test_value out varchar2
5 )
6 as
7 begin
8 -- your code goes here
9 null;
10 end;
11 /
Procedure created.
SQL>
This is how you call it (and get the error):
SQL> exec retrieve_decrypt(5, 'creditcardno');
BEGIN retrieve_decrypt(5, 'creditcardno'); END;
*
ERROR at line 1:
ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'RETRIEVE_DECRYPT'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
SQL>
The cause of the error is:
the procedure contains 3 parameters:
2 of them are IN - you provided their values
1 of them is OUT - you didn't provide it and got the error
Here's what you should have done: as the 3rd parameter is OUT, you'll have to DECLARE it:
SQL> declare
2 l_out varchar2(20);
3 begin
4 retrieve_decrypt(5, 'creditcardno', l_out);
5 end;
6 /
PL/SQL procedure successfully completed.
SQL>
EXEC you used is a SQL*Plus command so it might not work everywhere; DECLARE-BEGIN-END block will so I'd suggest you use it.
Alternatively, in SQL*Plus, it could be rewritten as
SQL> var l_out varchar2
SQL>
SQL> exec retrieve_decrypt(5, 'creditcardno', :l_out);
PL/SQL procedure successfully completed.
SQL>
but - once again - you'd better use DECLARE-BEGIN-END PL/SQL block.
The initial error is:
wrong number or types of arguments in call to 'RETRIEVE_DECRYPT'
The procedure requires 3 parameters. You are only passing 2 (or apparently only 1, in the attempt that generated the error message shown).
Why do you also see the message "Usually a PL/SQL compilation error"? The EXEC command in SQLPlus creates a PL/SQL block containing the text you provide, and sends that to Oracle for execution. Oracle attempts to compile that PL/SQL block (just like it compiles the procedure when you create it). In this case, the compilation fails because of the mismatch in the number of arguments.

How to write an Oracle procedure with a select statement (Specifically on SQL Developer)?

I want to create a simple Oracle Stored procedure on SQL Developer that will return some records on a simple select query. I do not want to pass in any parameter, but I just want the Records to be returned back from the procedure into a result set -> a suitable variable.
I have been trying to use the following syntax :
create or replace PROCEDURE Getmarketdetails2(data OUT varchar2)
IS
BEGIN
SELECT *
into data
from dual;
END Getmarketdetails2;
But it gives me an error while I try to execute with the following exec statement -->
Declare a Varchar2;
exec Getmarketdetails2(a);
Error: PLS-00103: Encountered the symbol "end-of-file" when expecting "something else".
Cause: Usually a PL/SQL compilation error.
Appreciate if anyone can help me out of this long pending situation! I have tried enough to find a basic guide to create a simple Oracle stored procedure and execute it in SQL Developer, but none of them answer to the point!!
You want:
DECLARE
a VARCHAR2(4000); -- Give it a size
BEGIN -- Begin the anonymous PL/SQL block
Getmarketdetails2(a); -- Call the procedure
DBMS_OUTPUT.PUT_LINE( a ); -- Output the value
END; -- End the anonymous PL/SQL block
/ -- End the PL/SQL statement
or:
VARIABLE a VARCHAR2(4000); -- Create a bind variable
EXEC Getmarketdetails2(:a); -- Execute the procedure using the bind variable
PRINT a -- Print the bind variable
Assuming an up-to-date Oracle version, you can use dbms_sql.return_result()
create or replace PROCEDURE Getmarketdetails2
IS
c1 SYS_REFCURSOR;
BEGIN
OPEN c1 FOR
SELECT *
from dual;
DBMS_SQL.RETURN_RESULT(c1);
END Getmarketdetails2;
/
Then simply run
exec Getmarketdetails2
The only drawback is that SQL Developer only displays the result as text, not as a proper result grid.
This is how I return a cursor in Oracle
PROCEDURE GetAllData (P_CURSOR OUT SYS_REFCURSOR)
IS
BEGIN
OPEN P_CURSOR FOR
SELECT *
FROM TABLE ;
END GetAllData ;
Declare a Varchar2;
exec Getmarketdetails2(a);
Your procedure is ok;
Instead of above query, use below query to run sp:
Declare
a Varchar2(10);
Begin
Getmarketdetails2(a);
End;

Handle & symbol in oracle stored procedure

I have an oracle stored procedure which accepts varchar2 input parameter. My problem is that some of the input parameter contains "&" or "<" sign. Since these are the special character Oracle ignores it.
Since this is stored procedure i can not do SET DEFINE OFF as it is called by some system.
Can you please help on this as i want to store data with this special character like "A & M Solution" or "hemil mistry"
Any help on this
You need to set DEFINE OFF in the caller, not in the procedure.
For example:
create or replace procedure doSomething(str in varchar2) is
begin
dbms_output.put_line(str);
end;
If I call this procedure from SQLPlus, I get:
SQL> exec doSomething('&&&');
&&&
PL/SQL procedure successfully completed.
SQL> exec doSomething('&aa');
Enter value for aa: XXX
XXX
PL/SQL procedure successfully completed.
After setting DEFINE OFF, I have:
SQL> set define off
SQL> exec doSomething('&aa');
&aa
PL/SQL procedure successfully completed.
create table s (str varchar2(4000));
create or replace procedure storestr(str in varchar2) is
begin
insert into s values(str);
commit;
end;
/
exec storestr('&&&');
exec storestr('&&<');
select * from s;
Everything works fine.
What is your problem? Maybe please post what your procedure is going to do with data containing special characters.
When you pass specials characters as input parameters you should use function
chr()
http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions019.htm
example:
set serveroutput on size 3000
create or replace procedure p_accept_special_char(ip_str in varchar2) is
begin
dbms_output.put_line(ip_str);
end;
/
exec p_accept_special_char(chr(38)||chr(62)||chr(60))
/
output:
SQL> set serveroutput on size 3000
SQL>
SQL> create or replace procedure p_accept_special_char(ip_str in varchar2) is
2 begin
3 dbms_output.put_line(ip_str);
4 end;
5 /
Procedure created.
SQL> exec p_accept_special_char(chr(38)||chr(62)||chr(60))
&><
PL/SQL procedure successfully completed.

Resources