Can we use Parameter mode directly inside BEGIN part? - oracle

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;

Related

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>

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>

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.

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.

use of bind variable

Can we use a bind variable in oracle inside a procedure or function ?
I'm trying to update a bind variable inside my procedure. Can I do so in any case?
if (condition) then
:v_bind:=10;
end if;
Can I do the above thing inside a procedure or function..?
variable v_bind number;
create procedure abc as v_one
BEGIN
select count(a) into v_one from ab;
if(v_one<>0) then
:v_bind:=10;
end if;
Will I able to do this? It is showing me bad variable v_bind
You can't create a procedure with a bind variable in it because stored procedures are server-side objects and bind variables only exist on the client side.
Suppose I'm using SQL*Plus, and that I've created some bind variables. Once I exit SQL*Plus, any bind variables I created don't exist any more. However, stored procedures have to persist in the database, and hence they can't have any reference to anything that was created and then destroyed on the client.
Here's an example showing that you can't create a procedure that references a bind variable:
SQL> variable i number
SQL> exec :i := 0;
PL/SQL procedure successfully completed.
SQL> print :i
I
----------
0
SQL> create or replace procedure test_proc
2 as
3 begin
4 :i := 9;
5 end;
6 /
Warning: Procedure created with compilation errors.
SQL> show errors procedure test_proc;
Errors for PROCEDURE TEST_PROC:
LINE/COL ERROR
-------- -----------------------------------------------------------------
4/3 PLS-00049: bad bind variable 'I'
You can, however, pass a bind variable as an OUT parameter for a procedure. The procedure can then assign a value to the OUT parameter, and this value will then be stored in your bind variable.
Suppose we have the following procedure:
CREATE OR REPLACE PROCEDURE do_stuff (
p_output OUT INTEGER
)
AS
BEGIN
p_output := 6;
END;
We can use this to set a bind variable as follows:
SQL> variable i number
SQL> exec :i := 0;
PL/SQL procedure successfully completed.
SQL> print :i
I
----------
0
SQL> exec do_stuff(:i);
PL/SQL procedure successfully completed.
SQL> print :i
I
----------
6
No, you cannot do what you are asking. Bind variables in plsql are handled transparently. You do not explicitly code bind variables unless you are going to use 'execute immediate' to run the code outside of plsql like this:
declare
v_bind number := 1;
begin
execute immediate 'select * from table where x = :v_bind';
end;`
The following code uses bind variables as well, but it is handled transparently by plsql:
declare
v_bind number := 1
y number;
begin
select count(*) into y from table where x = v_bind;
end;
You can't bind a sqlplus variable in a session to a function/procedure. It will give you error of "Bad bind variable". You can actually just pass bind variable from your oracle session to any procedure.
Let's see a example
variable v1 NUMBER;
begin
select salary into :v1 from employees where employee_id = 100;
dbms_output.put_line(:v1);
end;
/
And if you run the above example by enclosing in procedure/function it will show you error.
create or replace procedure proc is
begin
select salary into :v1 from employees where employee_id = 100;
dbms_output.put_line(:v1);
end;
/
Error -
PROCEDURE proc compiled
Warning: execution completed with warning
3/20 PLS-00049: bad bind variable 'V1'
4/22 PLS-00049: bad bind variable 'V1'
Thus, it is not possible to use session-level bind variables in procedures/functions. In below example t2 is a bind variable
create or replace procedure proc is
t2 NUMBER;
begin
select salary into t2 from employees where employee_id = 100;
dbms_output.put_line(t2);
end;
/
You can call this procedure from sqlplus as
exec proc;

Resources