I am just trying to achieve pipe row nested type. There are tons of examples around but none that I am able to apply.
My types are:
create type t1_row as object ( a1 number, a2 varchar2(10) );
create type t1_tab as table of t1_row;
create type t2_row as object ( b1 number, b2 varchar2(10), b3 t1_tab );
create type t2_tab as table of t2_row;
I've tried to create a function in so many ways, but none of them are able to compile successfully.
One example:
create or replace function fn (r in number) return t2_row pipelined is
l_row1 t1_tab;
l_row2 t2_tab;
begin
for i in 1..r loop
for j in 1..r loop
l_row1(j).a1 := j;
l_row1(j).a2 := 'a2 ' || j;
end loop;
l_row2(i) := (i,l_row1);
PIPE ROW (l_row2);
end loop;
return;
end;
This code produces the following errors:
[Error] PLS-00630 (1: 12): PLS-00630: pipelined functions must have a supported collection return type
[Error] PLS-00382 (10: 22): PLS-00382: expression is of wrong type
Any help advice or any similar example can be useful.
Version: Oracle 11g Release 11.2.0.1.0
Your construction for t2_row was missing the 2nd parameter, and you are
returning the wrong type. Try this:
create or replace function fn (r in number) return t2_tab pipelined is
l_row1 t1_tab := t1_tab();
l_row2 t2_tab := t2_tab();
begin
for i in 1..r loop
for j in 1..r loop
l_row1.extend(1);
l_row1(j) := t1_row(j,'a2 ' || j);
end loop;
l_row2.extend(1);
l_row2(i) := t2_row(i,'TEST',l_row1);
PIPE ROW (l_row2(i));
end loop;
return;
end;
Also, read this tutorial on how to use pl/sql object collections.
Your code has two syntax errors and a logic error.
The first syntax error is that the function's return type should be a collection not a row type, so
return t2_tab pipelined
The second syntax error is that you need to include the type when instantiating an object, and the number of arguments must match the signature of the type. So the outer loop assignment should be:
l_row2(i) := t2_row(i, 'T2', l_row1);
The logic error is that we don't need to maintain a collection variable for the output. We just need a row variable.
Also the indexed counts seem a bit confused, so my code may differ from your intention.
create or replace function fn (r in number)
return t2_tab pipelined
is
l_tab1 t1_tab;
l_row2 t2_row;
begin
for i in 1..r loop
l_tab1 := new t1_tab();
l_tab1.extend(r);
for j in 1..r loop
l_tab1(j) := t1_row(j*i, 'a2 ' || j);
end loop;
l_row2 := t2_row(i, 'T2', l_tab1);
PIPE ROW (l_row2);
end loop;
return;
end;
/
Here is the run:
SQL> select * from table(fn(3));
B1 B2 B3(A1, A2)
----- --- ---------------------------------------------------------------
1 T2 T1_TAB(T1_ROW(1, 'a2 1'), T1_ROW(2, 'a2 2'), T1_ROW(3, 'a2 3'))
2 T2 T1_TAB(T1_ROW(2, 'a2 1'), T1_ROW(4, 'a2 2'), T1_ROW(6, 'a2 3'))
3 T2 T1_TAB(T1_ROW(3, 'a2 1'), T1_ROW(6, 'a2 2'), T1_ROW(9, 'a2 3'))
SQL>
Related
My code:
create table info(str varchar2(30));
declare
cursor c(job emp_ast.job_id%type, dep emp_ast.department_id%type) is select employee_id
from emp_ast
where job_id=job and department_id=dep;
type t_job is table of emp_ast.job_id%type;
t t_job:=t_job();
emp emp_ast.employee_id%type;
i number(3);
begin
select job_id
bulk collect into t
from emp_ast;
for i in 10..270 loop
for j in 1..t.count loop
open c(i, t(j));
loop
fetch c into emp;
insert into info
values (i||' '||t(j)||' '||emp);
exit when c%notfound;
end loop;
i:=i+10;
end loop;
end loop;
end;
/
I get "expression 'I' cannot be used as an assignment target", reffering to the line where I increment i by 10. I am trying to save the department_id, employee_id and job_id as a string in a table for each department and each job.
At the point where you get that message, i refers to the loop control variable i defined in the line for i in 10..270 loop, not the int(3) variable defined earlier. In PL/SQL a loop definition defines a variable which is only accessible inside the loop, and which you cannot alter. I suggest you change the name of one or the other to make them unique.
EDIT
PL/SQL doesn't provide a way to step by more than 1 in a computed FOR loop. Instead, you will need to compute the desired department number value within the loop:
DECLARE
CURSOR c(job EMP_AST.JOB_ID%TYPE,
dep EMP_AST.DEPARTMENT_ID%TYPE)
IS SELECT EMPLOYEE_ID
FROM EMP_AST
WHERE JOB_ID = job AND
DEPARTMENT_ID = dep;
TYPE t_job IS TABLE OF EMP_AST.JOB_ID%TYPE;
t t_job := t_job();
emp EMP_AST.EMPLOYEE_ID%TYPE;
nDepartment NUMBER;
BEGIN
SELECT job_id
BULK COLLECT INTO t
FROM EMP_AST;
FOR i IN 1..27 LOOP
nDepartment := i * 10;
FOR j IN 1..t.COUNT LOOP
OPEN c(t(j), nDepartment);
LOOP
FETCH c INTO emp;
INSERT INTO info
VALUES (nDepartment || ' ' || t(j) || ' ' || emp);
EXIT WHEN c%notfound;
END LOOP; -- cursor c
CLOSE c;
END LOOP; -- j
END LOOP; -- i
END;
/
Note that in the code above the nDepartment value is computed within the i loop, which now increments from 1 to 27 instead of going from 10 to 270.
I want to put single clob data column value in 2 varchar2 columns by checking the length of CLOB column, but i am getting error in case statement, line is marked in * *, it says syntex error , what am i doing wrong
DECLARE
v_tot_rows NUMBER (3);
rqst_xml_1 ISG.CERT_TEST_CASE_GTWY_TXN.RQST_XML_1_TX%TYPE;
rqst_xml_2 ISG.CERT_TEST_CASE_GTWY_TXN.RQST_XML_2_TX%TYPE;
CURSOR req_res_populate_cur
IS
SELECT scptc.SWR_CERT_PRJCT_TEST_CASE_ID,
orb_txn.MIME_HEAD_TX,
orb_txn.RSPNS_XML_TX,
orb_msg.RQST_GNRL_VLD_JSON_TX,
orb_msg.RQST_TEST_CASE_VLD_JSON_TX,
orb_msg.MRCH_ID
(
CASE
WHEN DBMS_LOB.GETLENGTH (orb_txn.RQST_XML_TX) <= 4000 THEN
rqst_xml_1 := CAST ( orb_txn . RQST_XML_TX AS VARCHAR2 ( 4000 ) ) * ,
rqst_xml_2 := ''
WHEN DBMS_LOB.GETLENGTH(orb_txn.RQST_XML_TX)>4000 THEN
rqst_xml_1:=CAST(substr(orb_txn.RQST_XML_TX,1,4000) AS VARCHAR2(4000)),
rqst_xml_2:=CAST(substr(orb_txn.RQST_XML_TX,4001)
END
)
FROM ISG.online_messages msg
JOIN ISG.SWR_CERT_PRJCT_TEST_CASE scptc
ON msg.online_message_id = scptc.TXN_ID,
ISG.GTWY_PLTFM_TXN_MSG orb_msg
JOIN ISG.GTWY_PLTFM_TXN orb_txn
ON orb_msg.GTWY_PLTFM_TXN_ID = orb_txn.GTWY_PLTFM_TXN_ID
WHERE msg.SPEC_ID = 60;;
BEGIN
FOR req_res IN req_res_populate_cur
LOOP
DBMS_OUTPUT.PUT_LINE (req_res.SWR_CERT_PRJCT_TEST_CASE_ID,
req_res.MIME_HEAD_TX,
req_res.rqst_xml_1,
req_res.rqst_xml_2,
req_res.RSPNS_XML_TX,
req_res.RQST_GNRL_VLD_JSON_TX,
req_res.RQST_TEST_CASE_VLD_JSON_TX,
req_res.MRCH_ID);
END LOOP;
END;
Your problem is your invalid SELECT-statement. You're trying to set variables (of your plsql-block) within a query. That's not intended or allowed.
You need to select the values into columns. Here i added two columns. One for each xml-value.
SELECT scptc.SWR_CERT_PRJCT_TEST_CASE_ID,
orb_txn.MIME_HEAD_TX,
orb_txn.RSPNS_XML_TX,
orb_msg.RQST_GNRL_VLD_JSON_TX,
orb_msg.RQST_TEST_CASE_VLD_JSON_TX,
orb_msg.MRCH_ID,
CASE --Column-Start
WHEN DBMS_LOB.GETLENGTH (orb_txn.RQST_XML_TX) <= 4000
THEN
CAST (orb_txn.RQST_XML_TX AS VARCHAR2 (4000))
WHEN DBMS_LOB.GETLENGTH (orb_txn.RQST_XML_TX) > 4000
THEN
CAST (
SUBSTR (orb_txn.RQST_XML_TX, 1, 4000) AS VARCHAR2 (4000))
END
AS my_rqst_xml_1, -- Column-End. In this column you'll have the value for xml_1
CASE --Column-Start
WHEN DBMS_LOB.GETLENGTH (orb_txn.RQST_XML_TX) <= 4000
THEN
''
WHEN DBMS_LOB.GETLENGTH (orb_txn.RQST_XML_TX) > 4000
THEN
CAST (SUBSTR (orb_txn.RQST_XML_TX, 4001) AS VARCHAR2 (4000))
END
AS my_rqst_xml_2 -- Column-End. In this column you'll have the value for xml_12
FROM ISG.online_messages msg
JOIN ISG.SWR_CERT_PRJCT_TEST_CASE scptc
ON msg.online_message_id = scptc.TXN_ID,
ISG.GTWY_PLTFM_TXN_MSG orb_msg
JOIN ISG.GTWY_PLTFM_TXN orb_txn
ON orb_msg.GTWY_PLTFM_TXN_ID = orb_txn.GTWY_PLTFM_TXN_ID
WHERE msg.SPEC_ID = 60
Afterwards you can work with the result and get the values from it.
BEGIN
FOR req_res IN req_res_populate_cur
LOOP
DBMS_OUTPUT.PUT_LINE (req_res.SWR_CERT_PRJCT_TEST_CASE_ID,
req_res.MIME_HEAD_TX,
req_res.my_rqst_xml_1, -- here we can see the values
req_res.my_rqst_xml_2, -- here too
req_res.RSPNS_XML_TX,
req_res.RQST_GNRL_VLD_JSON_TX,
req_res.RQST_TEST_CASE_VLD_JSON_TX,
req_res.MRCH_ID);
-- And here we could store the values into variables or call some procedures etc.
rqst_xml_1 := req_res.my_rqst_xml_1;
rqst_xml_2 := req_res.my_rqst_xml_2;
END LOOP;
END;
I've to guess, but it seems you didn't want to declare the variables:
rqst_xml_1 ISG.CERT_TEST_CASE_GTWY_TXN.RQST_XML_1_TX%TYPE;
rqst_xml_2 ISG.CERT_TEST_CASE_GTWY_TXN.RQST_XML_2_TX%TYPE;
This would be only needed if you want to work with the values.
I have a table T1 and I want to insert multiple rows at a time through a procedure using collection. I have written the code but when I'm trying to execute it throws an error. Please advise.
create table t1 ( id number , name varchar2(10));
/
create or replace PACKAGE PKG1 AS
TYPE TAB_LIST IS TABLE OF T1%ROWTYPE;
PROCEDURE PROC1 (p_val IN TAB_LIST);
END PKG1;
/
create or replace PACKAGE BODY PKG1 AS
PROCEDURE PROC1 (P_VAL IN TAB_LIST
)
IS
BEGIN
FOR i IN p_val.FIRST..p_val.LAST
LOOP
insert INTO T1
(
id, name
)
VALUES
(
p_val(i).id,
p_val(i).name
);
END LOOP;
END;
END;
error after executing
DECLARE
p_val PKG1.TAB_LIST;
BEGIN
p_val := PKG1.TAB_LIST(123,'XYZ');
END;
Error report -
ORA-06550: line 5, column 11:
PLS-00306: wrong number or types of arguments in call to 'TAB_LIST'
ORA-06550: line 5, column 11:
PLS-00306: wrong number or types of arguments in call to 'TAB_LIST'
ORA-06550: line 5, column 2:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
DECLARE
P_VAL PKG1.TAB_LIST := PKG1.TAB_LIST();
BEGIN
P_VAL.extend;
P_VAL(1).id := 123;
P_VAL(1).name := 'XYZ';
PKG1.PROC1( P_VAL );
END;
example for multiple records:
DECLARE
P_VAL PKG1.TAB_LIST := PKG1.TAB_LIST();
BEGIN
for i in 1 .. 10
loop
P_VAL.extend;
P_VAL(P_VAL.LAST).id := i;
P_VAL(P_VAL.LAST).name := 'XYZ' || i;
end loop;
PKG1.PROC1( P_VAL );
END;
#hekko": its not a string .sorry about formatting . The multiple values (n) can be passed from application like this and all should be inserted into table at once.
123 'XYZ'
456 'DFK'
866 'HKK'
#Kaushik :it is not a string but formatting issue.. the question remains the same "I have a table T1 and I want to insert multiple rows at a time through a procedure using collection.
I have three PLSQL functions: A, B and C.
The idea is this: C is calling B, B is calling A.
Function A, when it's called by B, returns a numeric value as a status indicator AND a ref cursor with tabular results.
E.g. function_A (A1 in varchar2, A2 out sys_refcursor) return number;
Function B, when it receives the results from A, is expected to reformat the results before passes them on to C, also in a form of a ref cursor.
A is an existing function and it cannot be amended, while B and C will be completely new functions.
The question is, how do I fetch the ref cursor from A? I was able to get the numeric value returned by the function (i.e the status indicator), but I have problem fetching the results of the ref cursor from A.
If I'm calling A from B, can I assume that the ref cursor of A is automatically opened?
What are the logical steps to get the results from A's ref cursor? E.g. can I fetch the results into an object type?
P/S. I have very limited programming experience and am only few months new in PLSQL.
Any hints will be much appreciated.
Since you have not given us the code functions, we will be based on the description of your functions.
According to the description, you have 3 functions:
Function A.
create or replace function A(A1 in varchar2, A2 out sys_refcursor) return number is
begin
open A2 for select 1 from dual;
return 2;
end;
Function B.
create or replace function B(B1 out sys_refcursor) return number is
cur sys_refcursor;
res_A number;
row_ your_table_a%rowtype;
begin
res_A := A('',cur);
loop
fetch cur into row_;
exit when cur%notfound;
--proccess with row A
end loop;
open B1 for select 2 from dual;
return 1;
end;
Function C
create or replace function C() return number is
res_B number;
cur sys_refcursor;
row_ your_table_b%rowtype;
begin
res_B:= B(cur);
loop
fetch cur into row_;
exit when cur%notfound;
--proccess with row B
end loop;
return 2;
end;
Maybe you can try the below snippet. Tried to replicate the scenario you mentioned in the question. Hope this helps.
CREATE OR REPLACE FUNCTION A_TEST(
A1 IN VARCHAR2,
A2 OUT sys_refcursor )
RETURN NUMBER
AS
lv_num PLS_INTEGER;
BEGIN
NULL;
OPEN a2 FOR SELECT LEVEL FROM DUAL CONNECT BY LEVEL < 19;
RETURN 1;
END;
CREATE OR REPLACE FUNCTION B_TEST
RETURN sys_refcursor
AS
lv_cur sys_refcursor;
lv_num PLS_INTEGER;
BEGIN
lv_num:=A_TEST('AV',lv_cur);
RETURN lv_cur;
END;
CREATE OR REPLACE FUNCTION C_TEST
RETURN sys_refcursor
AS
tab PLS_INTEGER;
lv_cur sys_refcursor;
BEGIN
lv_cur:=B_TEST;
LOOP
FETCH lv_cur INTO tab;
EXIT
WHEN lv_cur%NOTFOUND;
dbms_output.put_line(tab);
END LOOP;
END;
I have function (used within a view) with the end result being a list of unique values from a row within a table of comma separated values.
Essentially given a table of:
studentid classes
12345 MATH 1301, HIST 1301, POLS 1301
57495 MATH 2309, HIST 1301
39485 MATH 1301, HIST 1301
I want to see
MATH 1301
MATH 2309
HIST 1301
POLS 1301
The below code works perfect if the source table is small, but when looking at a table of 30,000 rows I get the following error. ORA-06532: Subscript outside of limit
I'm pretty sure my problem is the collection is getting too large since it's getting duplicate values. The duplicate values in themselves only become a problem when the collection becomes too large. How do I keep the duplicate values out of the collection?
I've tried childnames.exists(element) but I believe this only works for seeing if there exists an element at the index value element correct? of I've looked at member of but I don't understand how to implement it.. Is there an easy way to check if a collection element exists? Or am I over looking something simple? Is there a different type other than odcivarchar2list that would allow a larger collection?
CREATE OR REPLACE FUNCTION unique_values_from_csv ( p_del VARCHAR2 := ',')
RETURN SYS.odcivarchar2list
IS
childnames SYS.odcivarchar2list := sys.odcivarchar2list ();
tempvar VARCHAR2(255);
l_idx PLS_INTEGER;
l_list2 VARCHAR2(32767) ;
l_value VARCHAR2(32767);
CURSOR tablewalker_cur
IS
SELECT distinct classes
FROM studentclasses;
BEGIN
FOR recordwalker_rec IN tablewalker_cur
LOOP
l_list2 := recordwalker_rec.classes;
LOOP
l_idx := INSTR (l_list2, p_del);
IF l_idx > 0
THEN
childnames.EXTEND;
tempvar := (LTRIM (RTRIM (SUBSTR (l_list2, 1, l_idx - 1))));
childnames (childnames.COUNT) := tempvar;
l_list2 := SUBSTR (l_list2, l_idx + LENGTH (p_del));
end if;
childnames.EXTEND;
childnames (childnames.COUNT) := (LTRIM (RTRIM (l_list2)));
EXIT;
END LOOP;
END LOOP;
RETURN childnames;
END unique_values_from_csv;
/
create or replace function unique_values_from_csv(p_del varchar2 := ',')
return sys.dbms_debug_vc2coll is
childnames sys.dbms_debug_vc2coll := sys.dbms_debug_vc2coll();
l_idx pls_integer;
l_list2 varchar2(32767) ;
procedure add_if_not_member(new_element varchar2) is
begin
if new_element not member of childnames then
childnames.extend;
childnames(childnames.count) := new_element;
end if;
end;
begin
for recordwalker_rec in (select distinct classes from studentclasses)
loop
l_list2 := recordwalker_rec.classes;
loop
l_idx := instr (l_list2, p_del);
if l_idx > 0 then
add_if_not_member(trim(substr (l_list2, 1, l_idx - 1)));
l_list2 := substr(l_list2, l_idx + length(p_del));
else
add_if_not_member(trim(l_list2));
exit;
end if;
end loop;
end loop;
return childnames;
end unique_values_from_csv;
/
Used SYS.DBMS_DEBUG_VC2COLL, which is a TABLE OF VARCHAR2(1000) and should support any number of elements. Although l_list2 varchar2(32767) will limit the results.
MEMBER OF is the correct condition.
Added an ELSE - the original function was only splitting the list in two.
Removed the cursor - for such a small query, another level of indirection isn't worth it.
Used TRIM() instead of LTRIM(RTRIM())
The best solution would be to throw out this function and stop storing non-atomic data in your database.
Here's some sample data and a query using the function:
create table studentclasses(studentid number, classes varchar2(4000));
insert into studentclasses
select 12345, 'MATH 1301,HIST 1301,POLS 1301' from dual union all
select 57495, 'MATH 2309,HIST 1301' from dual union all
select 39485, 'MATH 1301,HIST 1301' from dual;
commit;
select unique_values_from_csv from dual;
COLUMN_VALUE
MATH 1301
HIST 1301
POLS 1301
MATH 2309
select distinct
regexp_substr(classes, '([^,]+)(,\s*|$)', 1, occ, '', 1) as class
from
studentclasses,
(
select level as occ
from dual
connect by level <= (
select max(regexp_count(classes, ','))+1
from studentclasses
)
)
where
regexp_substr(classes, '([^,]+)(,\s*|$)', 1, occ, '', 1) is not null
order by 1