Handle & symbol in oracle stored procedure - oracle

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.

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;

What can be the smallest Procedure?

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>

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

Oracle: parameter default value not displayed

I wrote this procedure:
create or replace PROCEDURE num_mandati ( persona IN parlamento2018.parlamentari.cf%type default '1234567890ABCDEF') IS
nomePersona parlamentari.nome%type;
cognomePersona parlamentari.cognome%type;
n_codfis NUMBER := 0;
nessunParlamentare EXCEPTION;
BEGIN
dbms_output.put_line('persona: ' ||persona);
END num_mandati;
In the body of procedure there is an output instruction but the default value of the parameter 'persona' is not displayed.
The type of the column cf of the parlamentari's table is varchar2(16).
Why the parameter's value is not displayed?
Without knowing the way you call the procedure, I see two possible reasons:
you did not SET SERVEROUTPUT ON or you passed a NULL value:
SQL> exec num_mandati;
PL/SQL procedure successfully completed.
SQL> SET SERVEROUTPUT ON
SQL> exec num_mandati;
persona: 1234567890ABCDEF
PL/SQL procedure successfully completed.
SQL> exec num_mandati('');
persona:
PL/SQL procedure successfully completed.
SQL>

Is there a variable name that stores name of the stored procedure currently running?

Is there a variable name that stores name of the stored procedure currently running? Something similar to $0 in Unix.
CREATE OR REPLACE
PROCEDURE my_sproc(
param1 IN NUMBER,
)
AS
BEGIN
exec other_sproc(XXX);
END;
END;
XXX <- stores the string "my_sproc".
Depending on the Oracle version, you may be able to use conditional compilation and $$PLSQL_UNIT
If other_sproc just prints out the value passed in
create or replace procedure other_sproc( p_in in varchar2 )
as
begin
dbms_output.put_line( p_in );
end;
/
then in Oracle 11g, you can use $$PLSQL_UNIT in the caller
SQL> create or replace procedure my_sproc
2 as
3 begin
4 other_sproc( $$PLSQL_UNIT );
5 end;
6 /
Procedure created.
SQL> exec my_sproc;
MY_SPROC
PL/SQL procedure successfully completed.
This doesn't work as well when you are using packages though (and your stored procedures should almost always be in packages) because the $$PLSQL_UNIT will be the package name not the procedure name.
Note as well that you do not use EXEC in a PL/SQL block. EXEC is a SQL*Plus command. You simply call other_sproc like I do here.

Resources