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;
Related
HOW TO EXECUTE PACKAGED FUNCTION IN ANONYMOUS BLOCK?
CREATE OR REPLACE FUNCTION function_name(n_fdo number, n_perc varchar2,
n_rating varchar2,
n_oto varchar2,
n_rate varchar2,
n_vlore varchar2,
n_prdotto varchar2,
n_scrd varchar2,
n_retail varchar2,
n_porto varchar2)Return NUMBER;
That's function's description. It accepts number of parameters, all of them are obligatory (as they don't have default value).
As you want to all it from anonymous PL/SQL block, you'd declare a variable whose datatype matches the one returned by that function (it is NUMBER), pass parameters and - that's it.
declare
result number;
begin
result := function_name(n_fdo => 1,
n_perc => 'ABC',
...
n_porto => 'XYZ');
end;
/
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).
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.
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);
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;
/