New Oracle Package with Stored Procedure Including Parameters - oracle

I am having difficulty with writing my stored procedure within a package. Below is my stored procedure that compiles fine outside of the package, but I believe needs to be written differently within the package body. I admit this is my first time using a package...
create or replace PROCEDURE SP_COMMENT(P_MEMBER_ID IN VARCHAR2, P_MEMBER_LASTNAME IN
VARCHAR2, P_MEMBER_FIRSTNAME IN VARCHAR2, P_MEMBER_STARTDATE IN DATE,
P_MEMBER_ENDDATE IN DATE, P_PRODUCT_CAT_CODE IN VARCHAR2, P_COMMENT IN VARCHAR2,
COMMENT_CURSOR out sys_refcursor)
AUTHID CURRENT_USER
IS
BEGIN
EXECUTE IMMEDIATE
'INSERT INTO TEST
(
MEMBER_ID,
MEMBER_LASTNAME,
MEMBER_FIRSTNAME,
MEMBER_STARTDATE,
MEMBER_ENDDATE,
PRODUCT_CAT_CODE,
COMMENTS
)
VALUES
(
p_member_id,
p_member_lastname,
p_member_firstname,
p_member_startdate,
p_member_enddate,
p_product_cat_code,
p_comment)';
commit;
open COMMENT_CURSOR for select * from sconti.TEST;
END;
Below is the package that I started, and which is not working:
CREATE OR REPLACE
PACKAGE COMMENT_TEST IS
PROCEDURE SP_COMMENT(P_MEMBER_ID IN VARCHAR2, P_MEMBER_LASTNAME IN VARCHAR2,
P_MEMBER_FIRSTNAME IN VARCHAR2, P_MEMBER_STARTDATE IN DATE,
P_MEMBER_ENDDATE IN DATE, P_PRODUCT_CAT_CODE IN VARCHAR2, P_COMMENT IN VARCHAR2,
COMMENT_CURSOR out sys_refcursor) IS
BEGIN
EXECUTE IMMEDIATE
'INSERT INTO TEST
(
MEMBER_ID,
MEMBER_LASTNAME,
MEMBER_FIRSTNAME,
MEMBER_STARTDATE,
MEMBER_ENDDATE,
PRODUCT_CAT_CODE,
COMMENTS
)
VALUES
(
p_member_id,
p_member_lastname,
p_member_firstname,
p_member_startdate,
p_member_enddate,
p_product_cat_code,
p_comment)';
commit;
open COMMENT_CURSOR for select * from sconti.TEST;
END;
END COMMENT_TEST;
I look forward to any response to assist me....thanks!

I can't say for certain (because you haven't shared the error you're getting), but most basic error is a lack of understanding of the specification/body.
You've put the code into the package specification, rather than the body. The specification should just have the procedure declarations (i.e. no begin and end), where as the body has the full content of the procedure.
While it won't affect compilation, there is another problem: the SQL inside the string can't access the parameters supplied to the procedure. If you must use dynamic SQL (and there's absolutely no reason to in this case), then you need a using clause to bind the variable into the dynamic statement. In addition, making the SQL static will allow the SQL statement to be validated at compile-time, which has obvious advantages.
A revised packaged (specification and body) is below.
CREATE OR REPLACE PACKAGE comment_test IS
PROCEDURE sp_comment (p_member_id IN VARCHAR2,
p_member_lastname IN VARCHAR2,
p_member_firstname IN VARCHAR2,
p_member_startdate IN DATE,
p_member_enddate IN DATE,
p_product_cat_code IN VARCHAR2,
p_comment IN VARCHAR2,
comment_cursor OUT SYS_REFCURSOR);
END comment_test;
/
CREATE OR REPLACE PACKAGE BODY comment_test IS
PROCEDURE sp_comment (p_member_id IN VARCHAR2,
p_member_lastname IN VARCHAR2,
p_member_firstname IN VARCHAR2,
p_member_startdate IN DATE,
p_member_enddate IN DATE,
p_product_cat_code IN VARCHAR2,
p_comment IN VARCHAR2,
comment_cursor OUT SYS_REFCURSOR) IS
BEGIN
INSERT INTO test (member_id,
member_lastname,
member_firstname,
member_startdate,
member_enddate,
product_cat_code,
comments)
VALUES (p_member_id,
p_member_lastname,
p_member_firstname,
p_member_startdate,
p_member_enddate,
p_product_cat_code,
p_comment);
COMMIT;
OPEN comment_cursor FOR SELECT * FROM sconti.test;
END;
END comment_test;
/

Related

Return value from Oracle stored procedure

I have a stored procedure in Oracle. I want that if it updates the record successfully the it returns a return value 1 else 0. Let me know how can I do it. I am working with Angular, Asp.net, WebAPi and Oracle database.
Thanks in advance. My code is as follows:
CREATE OR REPLACE Procedure NML.AddProductProcedure(V_CRD_COD In varchar2,
V_ITM_COD In varchar2,
V_SRL_NUM In varchar2,
V_UOM_ABR IN varchar2,
V_QTD_RTE IN varchar2,
V_QTY_PRC In varchar2,
V_QTN_NUM IN varchar2,
V_PMT_FLG IN varchar2,
V_TAX_FLG IN varchar2,
V_DLV_FLG IN varchar2)
IS
BEGIN
UPDATE NML.pgi_00_13
SET UOM_ABR = V_UOM_ABR,
QTD_RTE = V_QTD_RTE,
QTY_PRC = V_QTY_PRC,
QTN_NUM = V_QTN_NUM,
PMT_FLG = V_PMT_FLG,
TAX_FLG = V_TAX_FLG,
DLV_FLG = V_DLV_FLG
WHERE
CRD_COD = V_CRD_COD
AND ITM_COD = V_ITM_COD
AND SRL_NUM = V_SRL_NUM;
END;
You can define an OUT variable from the procedure that gets set to 1 on successful completion or 0 from an EXCEPTION.
CREATE OR REPLACE PROCEDURE nml.addproductprocedure (
p_crd_cod IN VARCHAR2,
p_itm_cod IN VARCHAR2,
p_srl_num IN VARCHAR2,
p_uom_abr IN VARCHAR2,
p_qtd_rte IN VARCHAR2,
p_qty_prc IN VARCHAR2,
p_qtn_num IN VARCHAR2,
p_pmt_flg IN VARCHAR2,
p_tax_flg IN VARCHAR2,
p_dlv_flg IN VARCHAR2,
p_status OUT NUMBER --use this variable
)
IS
BEGIN
UPDATE nml.pgi_00_13
SET uom_abr = p_uom_abr,
qtd_rte = p_qtd_rte,
qty_prc = p_qty_prc,
qtn_num = p_qtn_num,
pmt_flg = p_pmt_flg,
tax_flg = p_tax_flg,
dlv_flg = p_dlp_flg
WHERE crd_cod = p_crd_cod AND itm_cod = p_itm_cod AND srl_num = p_srl_num;
p_status := 1;
EXCEPTION
WHEN OTHERS THEN
p_status := 0;
END;
/
Your calling block must define OUT parameters as shown here : How to return oracle output parameters from a stored procedure in .NET
What's your definition of "success"? At the moment if your procedure throws an exception it failed otherwise it succeeded. Is that not good enough?
You could extend the signature of the procedure with a parameter which tells you how many records were updated...
CREATE OR REPLACE Procedure NML.AddProductProcedure(
V_CRD_COD In varchar2,
V_ITM_COD In varchar2,
V_SRL_NUM In varchar2,
V_UOM_ABR IN varchar2,
V_QTD_RTE IN varchar2,
V_QTY_PRC In varchar2,
V_QTN_NUM IN varchar2,
V_PMT_FLG IN varchar2,
V_TAX_FLG IN varchar2,
V_DLV_FLG IN varchar2,
p_updated_cnt out pls_integer
)IS
BEGIN
Update NML.pgi_00_13
set
UOM_ABR=V_UOM_ABR,
QTD_RTE=V_QTD_RTE,
QTY_PRC=V_QTY_PRC,
QTN_NUM=V_QTN_NUM,
PMT_FLG=V_PMT_FLG,
TAX_FLG=V_TAX_FLG,
DLV_FLG=V_DLV_FLG
Where CRD_COD=V_CRD_COD
AND ITM_COD=V_ITM_COD
AND SRL_NUM=V_SRL_NUM;
-- count of rows updated by preceding statement
p_updated_cnt := sql%rowcount;
END;
Note that you could instead pass a flag or 1 or 0 as shown in #Kaushik solution but this is an anti-pattern. Because:
It returns success when the statement updates zero rows, which is a technical success but probably not a successful completion from a business perspective.
A calling program can choose to ignore the flag value, whereas it must handle a propagated exception.
If the calling program wants to act when the flag value indicates failure it has no idea why the called procedure failed. Exceptions on the other hand are usually specific enough to support decision making.
Just want retun 1 when it succeded to update the record
Well you could do this:
CREATE OR REPLACE Procedure NML.AddProductProcedure(
V_CRD_COD In varchar2,
V_ITM_COD In varchar2,
V_SRL_NUM In varchar2,
V_UOM_ABR IN varchar2,
V_QTD_RTE IN varchar2,
V_QTY_PRC In varchar2,
V_QTN_NUM IN varchar2,
V_PMT_FLG IN varchar2,
V_TAX_FLG IN varchar2,
V_DLV_FLG IN varchar2,
p_updated_flag out pls_integer
)IS
BEGIN
Update NML.pgi_00_13
set
UOM_ABR=V_UOM_ABR,
QTD_RTE=V_QTD_RTE,
QTY_PRC=V_QTY_PRC,
QTN_NUM=V_QTN_NUM,
PMT_FLG=V_PMT_FLG,
TAX_FLG=V_TAX_FLG,
DLV_FLG=V_DLV_FLG
Where CRD_COD=V_CRD_COD
AND ITM_COD=V_ITM_COD
AND SRL_NUM=V_SRL_NUM;
if sql%rowcount > 0 then
p_update_flag := 1;
else
p_update_flag := 0;
end if;
END;
This will still hurl if there's an exception - which is a good thing - but distinguishes between updating zero records and one record (or more).

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

Not passing an OUT parameter in PL/SQL

So I have this interesting situation. I have a PL/SQL procedure where I pass two IN parameters and two OUT parameters:
PROCEDURE p_merge_catalog(p_merge_from_code VARCHAR2,
p_merge_to_code VARCHAR2,
msg_type_out OUT VARCHAR2,
msg_out OUT VARCHAR2)
IS
.
.
My conundrum is I'm using a job submission/scheduler program to call the procedure that does not seem to deal with OUT variables. I'm not able to bind variables to the OUT parameters; it only passes values. Is there a way to call this procedure without specifying an OUT parameter? Or maybe trick it and use some kind of hidden variable?
Not very beautiful for two outs, but working ;)
CREATE OR REPLACE FUNCTION f_merge_catalog_wrapper (
p_merge_from_code VARCHAR2,
p_merge_to_code VARCHAR2)
RETURN emp%ROWTYPE
IS
msg_type_out VARCHAR2(2000);
msg_out VARCHAR2(2000);
BEGIN
p_merge_catalog(p_merge_from_code, p_merge_to_code, msg_type_out, msg_out);
return msg_type_out || '#' || msg_out;
END;
Didn't check for typos. But the mechanic should be clear :o).
If you don't need the output:
CREATE OR REPLACE PROCEDURE p_merge_catalog_wrapper (
p_merge_from_code VARCHAR2,
p_merge_to_code VARCHAR2)
RETURN emp%ROWTYPE
IS
msg_type_out VARCHAR2(2000);
msg_out VARCHAR2(2000);
BEGIN
p_merge_catalog(p_merge_from_code, p_merge_to_code, msg_type_out, msg_out);
END;

Oracle cast PL/SQL table to SYS_REFCURSOR

I am trying to access one my Stored Procedure from java code, where the procedure is returing a PL/SQL table(PACKAGE TABLE) type, as it is easy to handle SYS_REFCURSOR in my java code, I am trying to convert the PL/SQL table to SYS_REFCURSOR in my Stored Procedure. After googling I didn't got any appropriate answer for this conversion. Can someone help me out for this conversion logic?
create or replace PROCEDURE TESTPROC(
INPUT1 IN VARCHAR2,
INPUT2 IN VARCHAR2,
P_PRC OUT SYS_REFCURSOR) AS
PACKAGE_TABLE PACKAGE.TESTTABLE;
BEGIN
PACKAGE_TABLE := FUNCTION_RETURN_PACKAGE_TABLE(INPUT1, INPUT2);
-- **LOGIC TO CONVERT PACAKGE_TABLE TO SYS_REFCURSOR GOES HERE**
END TESTPROC;
You can use TABLE operator for this
create or replace PROCEDURE TESTPROC(
INPUT1 IN VARCHAR2,
INPUT2 IN VARCHAR2,
P_PRC OUT SYS_REFCURSOR) AS
BEGIN
OPEN P_PRC FOR
SELECT * FROM TABLE(FUNCTION_RETURN_PACKAGE_TABLE(INPUT1, INPUT2));
END TESTPROC;
But you should keep in mind that you have to have schema level pl\sql table type (for oracle <12c). Also notice that SELECT * FROM brings you one-feild rows with your-plsql-table-row-type value.

create/declare a sequence in a stored procedure in oracle

I have a table non_employee with emp_no as the primary key and a package with a procedure to insert in to this table.
I need to be able to autoincrement the emp_no when the procedure is run. I tried creating a sequence within the procedure like this, but getting errors. please see below and advice.
CREATE OR REPLACE PACKAGE BODY WFDDEV."ERD" IS
create SEQUENCE #seq_emp_nmbr;
PROCEDURE INS_NON_EMPLOYEES
(
in_DATE_ADDED DATE,
in_EMPLOYEE_NAME VARCHAR2,
in_DEPT_ID VARCHAR2,
in_SUB_DEPARTMENT VARCHAR2,
in_LOCATION VARCHAR2,
in_WORK_TEL_NO VARCHAR2,
in_TOUR VARCHAR2,
in_REST_DAYS VARCHAR2,
in_HOME_ADDRESS VARCHAR2,
in_CITY VARCHAR2,
in_STATE VARCHAR2,
in_ZIP VARCHAR2,
in_HOME_TEL_NO VARCHAR2,
in_GENDER VARCHAR2,
in_RACE VARCHAR2,
in_DATE_OF_BIRTH DATE,
in_AGE VARCHAR2,
in_HIRE_DATE DATE,
in_UNION_AFFILIATION VARCHAR2,
in_TITLE VARCHAR2,
in_NON_EE_INDICATOR VARCHAR2
) IS
BEGIN
INSERT INTO WFDDEV.NON_EMPLOYEES
(
EMP_NO,
DATE_ADDED,
EMPLOYEE_NAME,
DEPT_ID,
SUB_DEPARTMENT,
LOCATION,
WORK_TEL_NO,
TOUR,
REST_DAYS,
HOME_ADDRESS,
CITY,
STATE,
ZIP,
HOME_TEL_NO,
GENDER,
RACE,
DATE_OF_BIRTH,
AGE,
HIRE_DATE,
UNION_AFFILIATION,
TITLE,
NON_EE_INDICATOR
)
VALUES
(
emp_no.NEXTVAL,
in_DATE_ADDED,
in_EMPLOYEE_NAME,
in_DEPT_ID,
in_SUB_DEPARTMENT,
in_LOCATION,
in_WORK_TEL_NO,
in_TOUR,
in_REST_DAYS,
in_HOME_ADDRESS,
in_CITY,
in_STATE,
in_ZIP,
in_HOME_TEL_NO,
in_GENDER,
in_RACE,
in_DATE_OF_BIRTH,
in_AGE,
in_HIRE_DATE,
in_UNION_AFFILIATION,
in_TITLE,
in_NON_EE_INDICATOR
);
END;
Im getting PLS-00103: Encountered the symbol "CREATE" when expecting one of the following:
begin end function pragma procedure subtype type
error with this...
You need to create the sequence just once outside of the package as a separate database object. Then, in the insert statement in your package body you can reference the sequence to get the next value.
Try-
EXECUTE IMMEDIATE 'CREATE SEQUENCE SEQ_NAME START WITH 1 INCREMENT BY 1 MINVALUE 1 MAXVALUE 1000000 NOCYCLE NOCACHE ORDER';
This should be inside a Procedure or Function body and not in the Declaration section i.e. this should be treated as a executable statement.
Creating a sequence using Dynamic SQL is a bad idea and I am not sure why you would want to do that. However, if you are creating a sequence dynamically then remember to drop it as well after finishing up, using
EXECUTE IMMEDIATE 'DROP SEQUENCE SEQ_NAME';
This way you would atleast not run into errors (like ORA-00955) while calling the package procedure.

Resources