How to fetch the records from cursor which contains columns from different table? - oracle11g

FIRST PROCEDURE
create or replace PROCEDURE TESTPROCEDURE
(
P_LAF_PK IN NUMBER,
P_RET_VAL OUT SYS_REFCURSOR
) AS
BEGIN
OPEN p_RET_VAL FOR
SELECT LAF.AF_FEE,LAF.AF_FEES_PAYABLE,
LAH.LH_DECISION_DT,LAH.LH_ISSUED
FROM LH_APP_HDR LAH JOIN LIQ_APP_FEE LAF
ON AF_PK = LH_PK
WHERE LAF_APPID = P_LAF_PK;
END TESTPROCEDURE;
Calling this procedure in another procedure
SECOND PROCEDURE
create or replace PROCEDURE TESTPROCEDURE1
AS
BEGIN
DECLARE
V_LIQ_CURSOR SYS_REFCURSOR;
V_LIQ_CURSOR_OUT1 LIQ_APP_FEE%ROWTYPE;
BEGIN
TESTPROCEDURE (2727,V_LIQ_CURSOR);
LOOP
FETCH V_LIQ_CURSOR INTO V_LIQ_CURSOR_OUT1; -- getting error in this line like "Return
types of Result Set variables or query do not match"
EXIT WHEN V_LIQ_CURSOR%NOTFOUND;
IF(V_LIQ_CURSOR_OUT1.AF_FEE != 0) THEN
SELECT (V_LIQ_CURSOR_OUT1.AF_FEES_PAYABLE + V_LIQ_CURSOR_OUT1.AF_FEE) INTO V_TOTALFEE FROM DUAL;
END IF;
END TESTPROCEDURE1;
Can anyone tell me how to get the result from cursor which contains multiple table columns.
NOTE:
I want only two columns from the first procedure

Related

PL/SQL procedure, cursor, where clause

i ve got some database with customer id, names, surnames. I made procedure with some parameters, which includes cursor with parameters. And i want with that cursor show customer names, surnames, id. And i want show that, where i call procedure, and i for example write as parameters in procedure for example NULL NULL, it show me all record. Next if i write NULL, Surname, it show me records with that surnames. If i write firstname,NULL, it show record only records with that first name and if i write Firstname,lastname, it show me only that record which matching this. So my procedure works fine, but i need right where clause n cursor. This is my cursor
CREATE OR REPLACE PROCEDURE test_two(c_f_name VARCHAR2,c_l_name VARCHAR2) IS
CURSOR c2(f_name VARCHAR2,l_name VARCHAR2) IS
SELECT cus_id,cus_l_name,cus_f_name
FROM CUSTOMER
WHERE (cus_f_name IS NOT NULL AND (cus_f_name = f_name orcus_l_name
IS NULL))
AND (cus_l_name IS NOT NULL AND (cus_l_name = l_name or cus_f_name IS
NULL));
v_complex c2%ROWTYPE;
lv_show VARCHAR2(20);
f_name VARCHAR2(20) := c_f_name;
l_name VARCHAR2(20) := c_l_name;
BEGIN
open c2(f_name,l_name);
LOOP
FETCH c2 INTO v_complex;
dbms_output.put_line(v_complex.cus_id|| ' ' ||v_complex.cus_f_name|| ' '
||v_complex.cus_l_name);
EXIT WHEN c2%NOTFOUND;
END LOOP;
CLOSE c2;
END;
EVERYTHING IS IFNE IN THAT PROCEDURE BUT I NEED ONLY RIGHT WHERE CLAUSE! THANK YOU
You could use a combination of LIKE and NVL.
WHERE cus_f_name LIKE NVL(c_f_name, '%')
AND cus_l_name LIKE NVL(c_l_name, '%');
Explanation:
The function NVL returns the first parameter if it is not NULL.
Otherwise it returns the second parameter.

PL/SQL How to iterate and update from an query inside a loop

I don't know how to iterate and update from a query result inside the loop. Is it possible to loop again from the query inside my first loop? Here is my code:
CREATE OR REPLACE PROCEDURE "myTEST" (sp_type in char)
IS
CURSOR c1 IS
SELECT SP_ID FROM CI_SP
WHERE SP_TYPE_CD = sp_type;
sp_id char(10);
item_id_eq CI_SP_EQ.ITEM_ID_EQ%type;
BEGIN
FOR sp_rec in c1
LOOP
DBMS_OUTPUT.PUT_LINE(sp_rec.sp_id);
SELECT ITEM_ID_EQ INTO item_id_eq FROM CI_SP_EQ
WHERE SP_ID = sp_rec.sp_id;
DBMS_OUTPUT.PUT_LINE('item id eq :' || item_id_eq);
-- iterate here for each item_id_eq
-- execute update for each item_id_eq also
END LOOP;
END myTEST;
Instead of looping twice you could just do a join between CI_SP & CI_SP_EQ and get it done in one shot:
CREATE OR REPLACE PROCEDURE "myTEST"(sp_type IN CHAR) IS
BEGIN
FOR item IN (SELECT item_id_eq
FROM ci_sp_eq JOIN ci_sp USING (sp_id)
WHERE sp_type_cd = sp_type) LOOP
-- do your stuff.
NULL;
END LOOP;
END mytest;
I think you wouldn't even need a PL/SQL block, just a simple UPDATE will do, but I don't exactly know what you're trying to do.
Some other comments:
Don't create objects enclosed in "quotes", the object name is now case sensitive. In your case, the compilation will fail because you've created procedure name as "myTEST" and end it with mytest, which Oracle will treat it as "MYTEST" and you'll get compile error because of syntax check fail
Use VARCHAR2 instead of CHAR, CHAR will pad spaces if the input doesn't match the length specifier and will lead to further problems

Insert and delete in oracle 11g procedure

I need to write a procedure with input parameters like groupid and item ids(array)
I need to insert into a table with values(groupid,itemid[0]) all items like (1,11),(1,12),(1,13),etc. I have table with two columns (groupid,itemid). please help me to sort out this problem. I am trying to write merge but getting error
CREATE OR REPLACE PROCEDURE update_product_group_prc
(
in_product_group_key IN INT,
in_product_key_array IN dbms_utility.maxname_array
)
AS
indx pls_integer := in_product_key_array.FIRST;
BEGIN
WHILE(INDX IS NOT NULL)
LOOP
MERGE INTO DIM_PRODUCT_X_GROUP PXG
USING(SELECT IN_PRODUCT_GROUP_KEY ,
IN_PRODUCT_KEY_ARRAY(INDX) IN_PRODUCT_KEY_ARRAY
FROM DUAL) IN_TBL
ON(IN_TBL.IN_PRODUCT_GROUP_KEY=PXG.PRODUCT_GROUP_KEY)
WHEN MATCHED THEN
-- DELETE --need to delete source records which r not here in array.
-- WHERE
WHEN NOT MATCHED THEN
INSERT (PRODUCT_GROUP_KEY,PRODUCT_KEY)
VALUES (IN_TBL.IN_PRODUCT_GROUP_KEY,IN_TBL.IN_PRODUCT_KEY_ARRAY);`
INDX := IN_PRODUCT_KEY_ARRAY.NEXT(INDX);
END LOOP;
END update_product_group_prc;
Error(16,41): PL/SQL: ORA-00936: missing expression
You have a syntax error. Comment the "WHEN MATCHED THEN" fragment.
http://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_9016.htm#SQLRF55028

retrieve recordset with more than one column value using refcursor in oracle

am creating a package in pl/sql . with in this i declared the ref cursor . With in procedure am using select statement with multiple column name . but am not able to get the result.
here i attached my code. Help me to correct the error. Am new to pl/sql
code
CREATE OR REPLACE PACKAGE types AS
TYPE cursor_type IS REF CURSOR;
END Types;
/
CREATE OR REPLACE
PROCEDURE get_CDR_rs (p_no IN zkv.FLD_callingPartyNumber%TYPE,
CDR_recordset OUT SYS_REFCURSOR) AS
BEGIN
OPEN CDR_recordset FOR
SELECT FLD_callingPartyNumber,
FLD_dateTimeConnect
FROM CISCOCUIC_TBL
WHERE FLD_callingPartyNumber= p_no
ORDER BY FLD_callingPartyNumber,;
END get_CDR_rs;
/
SET SERVEROUTPUT ON SIZE 1000000
DECLARE
l_cursor SYS_REFCURSOR;
l_callingPartyNumber zkv.FLD_callingPartyNumber%TYPE;
l_dateTimeConnect zkv.FLD_dateTimeConnect%TYPE;
BEGIN
LOOP
FETCH l_cursor
INTO l_callingPartyNumber, l_dateTimeConnect;
EXIT WHEN l_cursor%NOTFOUND;
END LOOP;
CLOSE l_cursor;
END;
/
Error
9/41 PL/SQL: ORA-00936: missing expression
5/5 PL/SQL: SQL Statement ignored
First thing is there is a syntax error in the procedure. It should be
CREATE OR REPLACE
PROCEDURE get_CDR_rs (p_no IN zkv.FLD_callingPartyNumber%TYPE,
CDR_recordset OUT SYS_REFCURSOR) AS
BEGIN
OPEN CDR_recordset FOR
SELECT FLD_callingPartyNumber,
FLD_dateTimeConnect
FROM CISCOCUIC_TBL
WHERE FLD_callingPartyNumber= p_no
ORDER BY FLD_callingPartyNumber; -- there was a comma which is not required or you
-- missed a column
END get_CDR_rs;
/
Secondly where is get_CDR_rs being called to retrieve the results?
Thirdly why do you need the following? because you are using sys_refcursor
CREATE OR REPLACE PACKAGE types AS
TYPE cursor_type IS REF CURSOR;
END Types;
/
If you would like to see the results of your procedure which returns sys_refcursor, do as follows
variable rset refcursor;
DECLARE
p_no zkv.FLD_callingPartyNumber%TYPE;
BEGIN
p_no := '123';
get_CDR_rs (p_no, :rset);
END;
/
print rset

PL/SQL Inserting 1 row for each result in a select

I am writing a PL/SQL Procedure that performs a select based on input variables and then inserts a row for each result in the select. I am having trouble debugging what is wrong with my query due my newness to PL/SQL. I know this must be easy, but I am stuck here for some reason. Thanks for your help!
CREATE OR REPLACE PROCEDURE setup_name_map(ranking_id IN NUMBER, class_string IN VARCHAR2)
IS
BEGIN
FOR rec IN (SELECT NAME_ID FROM PRODUCT_NAMES WHERE NAME = class_string)
LOOP
EXECUTE IMMEDIATE 'INSERT INTO NAME_RANKING (NAME_ID, RANKING_ID) VALUES (' || rec.NAME_ID || ', ' || ranking_id || ')';
END LOOP;
END;
According to the Oracle Developer Compiler... 'NAME_ID' is an invalid identifier. I've tried putting it in quotes but no dice. It also complains that loop index variables 'REC' use is invalid. Any help is much appreciated.
There is no need for dynamic SQL here:
BEGIN
FOR rec IN (SELECT NAME_ID FROM PRODUCT_NAMES
WHERE NAME = class_string)
LOOP
INSERT INTO NAME_RANKING (NAME_ID, RANKING_ID)
VALUES (rec.NAME_ID, ranking_id);
END LOOP;
END;
Better still you can avoid a slow row-by-row cursor approach like this:
BEGIN
INSERT INTO NAME_RANKING (NAME_ID, RANKING_ID)
SELECT NAME_ID, ranking_id FROM PRODUCT_NAMES
WHERE NAME = class_string;
END;
If you really did need the dynamic SQL you should not be concatenating values into it, but using bind variables:
BEGIN
FOR rec IN (SELECT NAME_ID FROM PRODUCT_NAMES
WHERE NAME = class_string)
LOOP
EXECUTE IMMEDIATE 'INSERT INTO NAME_RANKING
(NAME_ID, RANKING_ID) VALUES (:b1, :b2)
USING rec.NAME_ID, ranking_id;
END LOOP;
END;

Resources