Oracle Package creation error - oracle

I created a package and a package body as below:
Create or replace Package pkg_1
as
procedure main(param1 varchar2, param2 varchar2, param3 int);
procedure one(param1 varchar2, param2 varchar2);
procedure two(param1 varchar2, param2 varchar2);
end pkg_1;
/
create or replace package body pkg_1
as
procedure main (param1 varchar2, param2 varchar2, param3 int)
is
v_sql_script varchar2(1000);
begin
case param3
when 1 then
v_sql_script := 'begin exec pkg_1.one(:param1,:param2); end;';
execute immediate v_sql_script using param1, param2;
end case;
end;
Now when I execute the package procedure with following statement:
exec pkg_1.main('p1','p2',1);
I got the following error:
ORA-06550: line 1, column 12:
PLS-00103: Encountered the symbol "PKG_1" when expecting one of the following:
:= . ( # % ;
The symbol ":=" was substituted for "PKG_1" to continue.
Can someone please suggest me what I have done wrong here?
Thank You.

It appears you're executing it inside a PL/SQL block, in which case you do not need the exec. Just do pkg_1.main('p1','p2',1); on its own.
Quite similar to part of this question.

Related

Encountered the symbol "end-of-file" - plsql

After executing it shows:
Compilation errors for PACKAGE BODY TEMP_PACKAGE
Error: PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
begin end function pragma procedure
Line: 17
create or replace package body TEMP_PACKAGE is
procedure insert_temp
(aaCode number,aaName varchar2, aaAddress varchar2,
aaPhone varchar2, aaState varchar2 )
is
begin
INSERT INTO temp_employee_azizbek
(code, aadName, aadAddress, aadPhone, aadState )
VALUES (aaCode, aaName,aaAddress,aaPhone,aaState);
end;
What is my mistake?
an extra end is missing, and you need package definition (header) besides package body :
create or replace package TEMP_PACKAGE is
procedure insert_temp(aaCode number,aaName varchar2, aaAddress varchar2,
aaPhone varchar2, aaState varchar2 );
end TEMP_PACKAGE;
/
create or replace package body TEMP_PACKAGE is
procedure insert_temp(aaCode number,aaName varchar2, aaAddress varchar2,
aaPhone varchar2, aaState varchar2 ) is
begin
INSERT INTO temp_employee_azizbek
(code, aadName, aadAddress, aadPhone, aadState )
VALUES (aaCode, aaName,aaAddress,aaPhone,aaState);
end;
end TEMP_PACKAGE;
/

Error PLS-00103 when create package with Oracle

I'm trying create package with oracle, although I have read many examples in docs oracle and built code same this tutorial but I still error this.
The following code:
create table manage_emplyee
(
f_name varchar(20),
l_name varchar(20)
);
// Specification
create or replace package fn2
as
procedure manage_emplyee(v_fname in VARCHAR2, v_lname in VARCHAR2);
procedure manage_emplyee_delete(v_fname in VARCHAR2);
end;
/
create or replace package body fn2
as
--Procedure Implementation
procedure manage_emplyee(v_fname in VARCHAR2, v_lname in VARCHAR2)
is
begin
insert into manage_emplyee VALUES (v_lname, v_lname);
end manage_emplyee;
// body
procedure manage_emplyee_delete (v_fname in VARCHAR2)
is
begin
delete manage_emplyee where v_fname = v_fname;
end manage_emplyee_delete;
end fn2;
/
Error
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
begin end function pragma procedure
Please help me fix it, thanks so much !
There are many things incorrect, so here is a correct version.
Run the table script first -
create table manage_emplyee
(
f_name varchar(20),
l_name varchar(20)
);
Run the Spec script after that
create or replace package fn2
as
procedure manage_emplyee(v_fname VARCHAR2,
v_lname VARCHAR2);
procedure manage_emplyee_delete(v_fname VARCHAR2);
end;
/
Run the body script after that
create or replace package body fn2
as
procedure manage_emplyee(v_fname VARCHAR2, v_lname VARCHAR2)
is
begin
insert into manage_emplyee VALUES (v_lname, v_lname);
end ;
procedure manage_emplyee_delete (v_fname VARCHAR2)
is
begin
delete from manage_emplyee where f_name = v_fname;
end ;
end;
/
The syntax for delete from table_name was incorrect.
I am sure, you want to match f_name from table with v_fname from the input variable to delete the data. Earlier you were matching v_fname with v_fname in your code, which will always be true (except for when its passed NULL) and you would end up loosing all your test data
*NOTE -
Adding IN explicitly is not required, the default type is IN for parameters in PLSQL procedures

How can I call stored procedure within a procedure and print the output in same line?

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

Strange PL/SQL error - PLS-00103

I have been getting a rather strange error for a long time in SQL developer.. I have stripped my package to the most basic and ran a variable declaration.. and even that is throwing an error.. this is what I am executing:
create or replace package body cdbmeta.pkg_metadata_check
is
procedure p_metadata_check(unit_id_start in number, unit_id_end in number)
is
begin
start_date NUMBER(10);
dbms_output.put_line('..');
end;
end;
and my error message states:
PLS-00103: Encountered the symbol "NUMBER" when expecting one of the following: := . ( # % ; The symbol ":=" was substituted for "NUMBER" to continue.
Totally clueless.. anyone had this before?
You have to put definition of variables after the "is" and before "begin" as follows:
create or replace package body cdbmeta.pkg_metadata_check
is
procedure p_metadata_check(unit_id_start in number, unit_id_end in number)
is
start_date NUMBER(10);
begin
dbms_output.put_line('..');
end;
end;
/
The block in procedure definition after is and before begin is the same as You would use with anonymous block like this:
declare
start_date NUMBER(10);
begin
dbms_output.put_line('..');
end;
/

Call procedure using anonymous block in pl/sql?

I am fairly new to PL/SQL but have been reading up on it and have used some templates, including some which I found from here.
What I want to do is to write an anonymous block to call some procedures which wrere written earlier in a sql developer project. I have attempted it but it isn't running properly. It returns an error of "Error starting at line : 2 in command " and then reports a "closed connection."
This is my attempt:
DECLARE
P_USER_NAME VARCHAR;
P_DEBUG_FLAG VARCHAR;
P_DEBUG_FIELD VARCHAR;
P_DEBUG_VALUE VARCHAR;
BEGIN
schema.package.procedure(
OutParam1, OutParam2, OutParam3, OutParam4);
dbms_output.put_line('OutParam1: ' || P_USER_NAME);
dbms_output.put_line('OutParam2: ' || P_DEBUG_FLAG);
dbms_output.put_line('OutParam3: ' || P_DEBUG_FIELD);
dbms_output.put_line('OutParam4: ' || P_DEBUG_VALUE);
END;
/
And these are the procedure I want to call:
PROCEDURE CLEAR_DEBUG (P_USER_NAME IN VARCHAR2);
PROCEDURE WRITE_DEBUG (P_USER_NAME IN VARCHAR2,
P_DEBUG_FLAG IN VARCHAR2,
P_DEBUG_FIELD IN VARCHAR2,
P_DEBUG_VALUE IN VARCHAR2);
PROCEDURE READ_DEBUG (P_USER_NAME IN VARCHAR2,
P_REF_CURSOR OUT SYS_REFCURSOR);
END P_DEBUG;
There are more errors in your code:
declare the variables properly - VARCHAR requires length constraint
OutParam1, OutParam2, OutParam3, OutParam4 are not declared - use the declared variables as arguments instead, beware of the variables passed as arguments must have the same data type
I expect you wanted to call P_DEBUG.WRITE_DEBUG(P_USER_NAME, P_DEBUG_FLAG, P_DEBUG_FIELD, P_DEBUG_VALUE);

Resources