I know this is a very stupid question, but I just can't get this to work. I get an error saying that identifier hello must be declared
CREATE OR REPLACE PROCEDURE hello (p_name IN VARCHAR2)
IS
BEGIN
dbms_output.put_line (‘Welcome '|| p_name);
END hello;
/
EXECUTE hello('JOHN');
Everything's fine, except the first single quote in DBMS_OUTPUT call:
SQL> CREATE OR REPLACE PROCEDURE hello (p_name IN VARCHAR2)
2 IS
3 BEGIN
4 dbms_output.put_line ('Welcome '|| p_name);
5 END hello; -- ^ change this one
6 /
Procedure created.
SQL> EXECUTE hello('JOHN');
Welcome JOHN
PL/SQL procedure successfully completed.
SQL>
Related
So, in this code what exactly PROMPT syntax do?
PROMPT create or replace procedure abc (p_name, p_id)
AUTHID CURRENT_USER
as
begin
dbms_output.put_line('hi');
end;
PROMPT has a meaning if you run some code in SQL*Plus (not many people do that nowadays). It displays text what follows that keyword.
SQL> help prompt
PROMPT
------
Sends the specified message or a blank line to the user's screen.
PRO[MPT] [text]
SQL> prompt hello there!
hello there!
SQL>
In your case, it produces unwanted result as it displays the create procedure (instead of creating it):
SQL> PROMPT create or replace procedure abc (p_name, p_id)
create or replace procedure abc (p_name, p_id)
SQL> AUTHID CURRENT_USER
SP2-0734: unknown command beginning "AUTHID CUR..." - rest of line ignored.
SQL> as
SP2-0042: unknown command "as" - rest of line ignored.
SQL> begin
2 dbms_output.put_line('hi');
3 end;
4 /
hi
PL/SQL procedure successfully completed.
SQL>
You got the result, but just as pure accident as
begin
dbms_output.put_line('hi');
end;
was a valid PL/SQL block.
Code you posted (without prompt) is invalid:
SQL> create or replace procedure abc (p_name, p_id)
2 AUTHID CURRENT_USER
3 as
4 begin
5 dbms_output.put_line('hi');
6 end;
7 /
Warning: Procedure created with compilation errors.
SQL> show err
Errors for PROCEDURE ABC:
LINE/COL ERROR
-------- -----------------------------------------------------------------
1/22 PLS-00103: Encountered the symbol "," when expecting one of the
following:
in out <an identifier> <a double-quoted delimited-identifier>
... long double ref char time timestamp interval date binary
national character nchar
3/1 PLS-00103: Encountered the symbol "AS" when expecting one of the
following:
with authid cluster order deterministic parallel_enable
pipelined result_cache
6/4 PLS-00103: Encountered the symbol "end-of-file" when expecting
one of the following:
end not pragma final instantiable order overriding static
member constructor map
SQL>
What does it mean? Procedure's parameters have to have datatype:
SQL> create or replace procedure abc (p_name in varchar2, p_id in number)
2 AUTHID CURRENT_USER
3 as
4 begin
5 dbms_output.put_line('hi');
6 end;
7 /
Procedure created.
SQL> exec abc(null, null);
hi
PL/SQL procedure successfully completed.
SQL>
I have written below procedure in oracle but its getting compiled with error.
CREATE OR REPLACE PROCEDURE PROC_MIGRATE_ONEIBER
AS
BEGIN
-- EXECUTE PROCEDURE ONEFIBER_LNK_MIGRATE;
execute ONEFIBER_LNK_MIGRATE;
execute ONEFIBER_LNK_MIGRATE_CITY;
execute ONEFIBER_LNK_MIGRATE_NLDC;
END;
-- NULL;
END PROC_MIGRATE_ONEIBER;
I have to execute 3 procedures in one main procedure without any IN or OUT parameters. But its not working
Just remove the execute keyword. Whenever you want to call another procedure within the procedure you can directly use the procedure name and parameters, If any.
CREATE OR REPLACE PROCEDURE PROC_MIGRATE_ONEIBER AS
BEGIN
-- EXECUTE PROCEDURE ONEFIBER_LNK_MIGRATE;
ONEFIBER_LNK_MIGRATE;
ONEFIBER_LNK_MIGRATE_CITY;
ONEFIBER_LNK_MIGRATE_NLDC;
--END;
-- NULL;
END PROC_MIGRATE_ONEIBER;
/
Update: Check this small code of how it should be. All procedures created in this answer are just for demo. (Make sure that the procedure that you are calling is not INVALID)
SQL>
SQL> CREATE OR REPLACE PROCEDURE ONEFIBER_LNK_MIGRATE AS
2 BEGIN
3 NULL;
4 END;
5 /
Procedure created.
SQL>
SQL> CREATE OR REPLACE PROCEDURE ONEFIBER_LNK_MIGRATE_CITY AS
2 BEGIN
3 NULL;
4 END;
5 /
Procedure created.
SQL>
SQL> CREATE OR REPLACE PROCEDURE ONEFIBER_LNK_MIGRATE_NLDC AS
2 BEGIN
3 NULL;
4 END;
5 /
Procedure created.
SQL>
SQL> CREATE OR REPLACE PROCEDURE PROC_MIGRATE_ONEIBER AS
2 BEGIN
3 ONEFIBER_LNK_MIGRATE;
4 ONEFIBER_LNK_MIGRATE_CITY;
5 ONEFIBER_LNK_MIGRATE_NLDC;
6 END PROC_MIGRATE_ONEIBER;
7 /
Procedure created.
SQL>
I am using Oracle 12c database and trying to run a package using SQL commands.
CREATE OR REPLACE PACKAGE "PK_CP_OTM" as
FUNCTION F_CP_OPTIMIZATION (
v_current_day IN VARCHAR2,
v_branch_code IN VARCHAR2)
RETURN VARCHAR2;
END PK_CP_OTM;
When I try to execute it using:
DECLARE
BEGIN
EXECUTE IMMEDIATE PK_CP_OTM.F_CP_OPTIMIZATION('20190409','BRNCD001');
END;
It shows:
ORA-00900: invalid SQL statement
ORA-06512: at line 3
00900. 00000 - "invalid SQL statement"
Thanks for your help.
As #Littlefoot said, you don't need dynamic SQL here, you can make a static call; but as you are calling a function you do need somewhere to put the result of the call:
declare
l_result varchar2(30); -- make it a suitable size
begin
l_result := pk_cp_otm.f_cp_optimization('20190409','BRNCD001');
end;
/
In SQL*Plus, SQL Developer and SQLcl you can use the execute client command (which might have caused some confusion) and a bind variable for the result:
var result varchar2(30);
exec :result := pk_cp_otm.f_cp_optimization('20190409','BRNCD001');
print result
There's nothing dynamic here, so - why would you use dynamic SQL at all?
Anyway: if you insist, then you'll have to select the function into something (e.g. a local variable). Here's an example
First, the package:
SQL> set serveroutput on
SQL>
SQL> create or replace package pk_cp_otm
2 as
3 function f_cp_optimization (v_current_day in varchar2,
4 v_branch_code in varchar2)
5 return varchar2;
6 end pk_cp_otm;
7 /
Package created.
SQL> create or replace package body pk_cp_otm
2 as
3 function f_cp_optimization (v_current_day in varchar2,
4 v_branch_code in varchar2)
5 return varchar2
6 is
7 begin
8 return 'Littlefoot';
9 end;
10 end pk_cp_otm;
11 /
Package body created.
How to call the function?
SQL> declare
2 l_result varchar2 (20);
3 begin
4 execute immediate
5 'select pk_cp_otm.f_cp_optimization (''1'', ''2'') from dual'
6 into l_result;
7
8 dbms_output.put_line ('result = ' || l_result);
9 end;
10 /
result = Littlefoot
PL/SQL procedure successfully completed.
SQL>
How can I call a stored procedure within a procedure and print the output in same line.
For example my full name is 'Alex Bob' and I have created 2 procedure each for firstname(p1) and lastname(p2) like this:
-- Procedure 1:
CREATE OR REPLACE PROCEDURE p1(fn in out varchar)
IS
BEGIN
dbms_output.put_line(fn);
END;
-- Procedure 2:
CREATE OR REPLACE PROCEDURE p2(ln in out varchar)
IS
BEGIN
p1('Alex');
dbms_output.put_line(ln);
END;
-- Calling procedure
exec p2('Bob');
Now this will give me output like this:
Alex
Bob
But I want to print name together (in a single line) and for this I tried calling procedure within procedure by creating local variable and calling procedure & storing returned value in that variable. Here is my revised code:
CREATE OR REPLACE PROCEDURE p1(fn in varchar)
IS
BEGIN
declare
cn varchar;
cn := exec p2('Bob');
dbms_output.put_line(fn || cn);
END;
CREATE OR REPLACE PROCEDURE p2(ln in out varchar)
IS
BEGIN
ln := ln;
END;
exec p1('Alex');
but this doesn't work as expected. How can I achieve this ?
You can create a Private Procedure to achieve this. See below:
CREATE OR REPLACE PROCEDURE p1 (fn IN VARCHAR)
AS
v_nam varchar2(100):='Bob';
--private Procedure
PROCEDURE p2 (LN IN OUT VARCHAR)
IS
BEGIN
null;
END;
BEGIN
p2(lN => v_nam);
DBMS_OUTPUT.put_line (fn ||' '||v_nam);
END;
Execution:
SQL> exec p1('Alex');
Alex Bob
PL/SQL procedure successfully completed.
First of all, literals cannot be passed as in OUT parameters - only IN, simply because you cannot assign anything to a literal, can you? Now back to the problem at hand. If you need those two procedures be able to print on the same line, use dbms_output.put() in all procedures but the last one and in the last one call dbms_output.put_line(). here is an example:
CREATE OR REPLACE PROCEDURE p1(fn in varchar2)
IS
BEGIN
dbms_output.put(fn);
END;
/
-- Procedure 2:
CREATE OR REPLACE PROCEDURE p2(ln in varchar2)
IS
BEGIN
p1('Alex ');
dbms_output.put(ln);
END;
/
create or replace procedure p3(ln in varchar2)
is
begin
p2('Bob ');
dbms_output.put_line(ln);
end;
exec p3('is a nice guy')
Result:
Alex Bob is a nice guy
PL/SQL procedure successfully completed
Find out more
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.