fetching a string and erase duplicates - plsql

I have a cursor called cACLsonDistribution that I fetch into the variable called ACLs.
The cursor is populated with a list of emails and sometimes those emails are duplicated.
LOOP
FETCH cACLsonDistribution INTO ACLs;
EXIT WHEN cACLsonDistribution%NOTFOUND;
ACLsonDistribution := ACLsonDistribution || ','|| ACLs.ENGINEER_EMAIL;
END LOOP;
This returns a string like this:
,bernardo.t#example.com,bernardo.t#example.com,felipe.t#example.com,jose.t#example.com
As you can see there 2 duplicates emails there. Sometimes there are 3 duplicates emails. What I would like to return the string without the duplicates. Like this:
,bernardo.t#example.com,felipe.t#example.com,jose.t#example.com

Try this simple illustration. Hope this helps.
SET SQLBL ON;
SET serveroutput ON;
DECLARE
PLST SYS_REFCURSOR;
AV VARCHAR2(100);
LV_VAR VARCHAR2(100);
VAR_TAB VARCHAR2(100);
BEGIN
OPEN PLST FOR SELECT 'avrajit.roy#abc.com' nm FROM DUAL
UNION ALL
SELECT 'avrajit.roy#abc.com' nm FROM DUAL
UNION ALL
SELECT 'shubhojit.roy#abc.com' nm FROM DUAL;
LOOP
FETCH plst INTO AV;
EXIT
WHEN PLST%NOTFOUND;
-- NULL;
lv_var:=lv_var||','||av;
END LOOP;
-- close
DBMS_OUTPUT.PUT_LINE(SUBSTR(LV_VAR,2,LENGTH(LV_VAR)));
SELECT WMSYS.WM_CONCAT(a.txt)
INTO var_tab
FROM
( SELECT DISTINCT(REGEXP_SUBSTR(SUBSTR(LV_VAR,2,LENGTH(LV_VAR)),'[^,]+', 1, level)) txt
FROM DUAL
CONNECT BY regexp_substr(SUBSTR(LV_VAR,2,LENGTH(LV_VAR)), '[^,]+', 1, level) IS NOT NULL
)a;
DBMS_OUTPUT.PUT_LINE(var_tab);
END;
----------------------------------OUTPUT----------------------------------------
avrajit.roy#abc.com,shubhojit.roy#abc.com
----------------------------------OUTPUT----------------------------------------

Related

Does Oracle support non-scalar cursor parameter?

This is a question about Oracle PL/SQL.
I have a procedure in which the exact WHERE clause is not known until the run time:
DECLARE
CURSOR my_cursor is
SELECT ...
FROM ...
WHERE terms in (
(SELECT future_term2 FROM term_table), -- whether this element should be included is conditional
(SELECT future_term1 FROM term_table),
(SELECT present_term FROM term_table)
);
BEGIN
(the processing)
END;
/
What the (SELECT ... FROM term_table) query returns is a 4-character string.
For a solution to this, I am thinking of using a parameterized cursor:
DECLARE
target_terms SOME_DATATYPE;
CURSOR my_cursor (pi_terms IN SOME_DATATYPE) IS
SELECT ...
FROM ...
WHERE terms in my_cursor.pi_terms;
BEGIN
target_terms := CASE term_digit
WHEN '2' THEN (
(SELECT future_term2 FROM term_table),
(SELECT future_term1 FROM term_table),
(SELECT present_term FROM term_table)
) ELSE (
(SELECT future_term1 FROM term_table),
(SELECT present_term FROM term_table)
)
END;
FOR my_record IN my_cursor (target_terms) LOOP
(the processing)
END LOOP;
END;
/
The problem is what the datatype for SOME_DATATYPE should be is not known to me, nor is it known whether Oracle supports such a cursor parameter at all. If supported, is the way shown above to fabricate the value for target_terms correct? If not, how?
Hope someone who know can advise. And thanks a lot for the help.
You can certainly pass a parameter to a cursor, just like you can to a function - but only IN parameters. However, PL/SQL is a strongly typed language, so the datatype must be specified at the time of compilation.
It looks to me like what you will need to do is construct the query dynamically and then use
OPEN cursor FOR l_query;
where l_query is the constructed string. This should give you a feel for what you can do:
CREATE OR REPLACE PACKAGE return_id_sal
AUTHID DEFINER
IS
TYPE employee_rt IS RECORD
(
employee_id employees.employee_id%TYPE,
salary employees.salary%TYPE
);
FUNCTION allrows_by (append_to_from_in IN VARCHAR2 DEFAULT NULL)
RETURN SYS_REFCURSOR;
END return_id_sal;
/
CREATE OR REPLACE PACKAGE BODY return_id_sal
IS
FUNCTION allrows_by (append_to_from_in IN VARCHAR2 DEFAULT NULL)
RETURN SYS_REFCURSOR
IS
l_return SYS_REFCURSOR;
BEGIN
OPEN l_return FOR
'SELECT employee_id, salary FROM employees ' || append_to_from_in;
RETURN l_return;
END allrows_by;
END return_id_sal;
/
DECLARE
l_cursor SYS_REFCURSOR;
l_row return_id_sal.employee_rt;
BEGIN
l_cursor := return_id_sal.allrows_by ('WHERE department_id = 10');
LOOP
FETCH l_cursor INTO l_row;
EXIT WHEN l_cursor%NOTFOUND;
END LOOP;
END;
/
You will need to take precautions against SQL injection with this sort of code. Certainly a user should never be able to pass SQL text directly to such a function!
You can use also some built-in VARRAY SQL types like SYS.ODCIVARCHAR2LIST or create your own :
CREATE OR REPLACE NONEDITIONABLE TYPE VARCHARLIST
AS VARRAY(32767) OF VARCHAR2(4000);
Then you can use it with SELECT COLUMN_VALUE FROM TABLE(COLLECTION) statement in your cursor:
DECLARE
l_terms SYS.ODCIVARCHAR2LIS; --or VARCHARLIST
CURSOR my_cursor (p_terms IN SYS.ODCIVARCHAR2LIS) IS
SELECT your_column
FROM your_table
WHERE terms in (select COLUMN_VALUE from table (p_terms));
BEGIN
select term
bulk collect into l_terms
from (
select 'term1' term from dual
union all
select 'term2' term from dual
);
FOR my_record IN my_cursor (l_terms) LOOP
--process data from your cursor...
END LOOP;
END;

cursor value in other select query

create or replace PROCEDURE "RESULT" (res1 OUT SYS_REFCURSOR )
IS
Cursor c is Select distinct id,fname,lname,dob,gender,address1 from emp where name like '%B%'
d c%rowtype;
BEGIN
OPEN c;
LOOP
fetch c into d;
exit when c%notfound;
OPEN res1 FOR select e.id from emp e where e.poi_user_id IN (d.id);
End Loop;
END;
Procedure RESULT compiled.
if i run query without procedure i get 5 results but when i am using the above code, it only returns the last result.
SET SERVEROUTPUT ON;
Declare
c SYS_REFCURSOR;
id number;
begin
RESULT(c);
loop
fetch c into id; -- and other columns if needed
exit when c%notfound;
dbms_output.put_line(id);
end loop;
END;
Result 5
You shouldn't make the coding difficult for yourself to understand. You dont need any loop here in this case a simple SELECT with FILTER condition will be enough to suffice your requirement. Hope below query helps. Also its not a good coding practice to user "" for naming convention.
As far as your question is concerned REFCURSOR is not that intelligent to keep all the records for each iteration and print you the collaborated output.
CREATE OR REPLACE PROCEDURE "RESULT"(
res1 OUT SYS_REFCURSOR )
IS
BEGIN
OPEN res1 FOR
SELECT e.id FROM emp e
WHERE EXISTS
( SELECT 1
FROM emp E1
WHERE e1.name LIKE '%B%'
AND e1.poi_user_id = e.id
);
END;

how to Pass table name to PL SQL cursor dynamically?

I have written one SQL Procedure where I have written one cursor and every time i have to pass table name to cursor query dynamically .
create or replace
PROCEDURE Add_DEN as
v_TableName VARCHAR2(4000) := 'BO_USER_DATA';
cursor c_DEN is select * from BO_USER_DATA; // Want to pass dynamically ,now hardcoded
r_DEN c_DEN%ROWTYPE;
fetch c_DEN into r_DEN;
v_Name := r_DEN."Name";
Can i write something like this
cursor c_DEN is "select * from " || v_TableName;
Any Help ?
here an example:
declare
TYPE curtype IS REF CURSOR;
l_cursor curtype;
l_param number;
l_key number;
l_value number;
l_sql varchar2(200);
begin
/* build your sql... */
l_sql := 'with data as (select 1 key, 100 value from dual union select 2, 200 from dual union select 3, 300 from dual union select 3, 301 from dual)' ||
' select key, value from data where key = :1';
l_param := 3;
open l_cursor for l_sql
using l_param;
loop
fetch l_cursor
into l_key, l_value;
exit when l_cursor%notfound;
dbms_output.put_line(l_key||' = '||l_value);
end loop;
close l_cursor;
end;
Result:
3 = 300
3 = 301
The basic answer is yes, you can and Given your example I would recommend you to use execute immediate to execute an arbitrary SQL string and bind the variables.
I would nevertheless reconsider if you really needed to dynamically set the table as this is not very often really needed.
Example:
DECLARE
sql_stmt VARCHAR2(200);
emp_id NUMBER(4) := 7566;
emp_rec emp%ROWTYPE;
BEGIN
sql_stmt := 'SELECT * FROM emp WHERE empno = :id';
EXECUTE IMMEDIATE sql_stmt INTO emp_rec USING emp_id;
END;

Cursor inside cursor and how pass variable

I need to make two cursors or one cursor and a loop. But one must be inside other. I'm trying with two cursors and with a for but don't recognize me the variable value of name table of the outside cursor.
Could you help me? Could you suggest me the best way to do these? I'm newbie with pl/sql. Thanks. I put the code:
DECLARE
TABLENAME VARCHAR2(30);
LINK VARCHAR2(30);
CURSOR FIRST_SELECT IS
SELECT A.TABLE_NAME AS TABLENAME, B.LINK AS LINK
FROM ALL_TABLES A, TABLE_CONNECT_LINK B
WHERE A.TABLE_NAME=B.COMPARE_TABLE;
BEGIN
OPEN FIRST_SELECT;
LOOP
FETCH FIRST_SELECT INTO TABLENAME,LINK;
EXIT WHEN FIRST_SELECT%NOTFOUND;
DECLARE
PEPELU VARCHAR2(4000);
TABLE VARCHAR2(70);
stringconsult VARCHAR2(4000);
COMPONENTE NUMBER(5);
TABLE_INSERTAR VARCHAR2(30);
COLUMN VARCHAR2(30);
OWNER VARCHAR2(30);
CURSOR ALL IS
SELECT TABLE,COLUMN,OWNER FROM TABLENAME;
BEGIN
OPEN ALL;
LOOP
FETCH ALL INTO TABLE,COLUMN,OWNER;
EXIT WHEN ALL%NOTFOUND;
stringconsult:='select ' || owner||','|| TABLE||','|| COLUMN||' from ' || INSERTNAME || ' minus select owner, table_name, column_name from all_tab_columns#'|| LINK||'';
DBMS_OUTPUT.PUT_LINE(stringconsult || ';');
DBMS_OUTPUT.PUT_LINE('COMMIT;');
COMMIT;
END LOOP;
CLOSE ALL;
END;
END LOOP;
CLOSE FIRST_SELECT;
END;
/

Can I pass an explicit cursor to a function/procedure for use in FOR loop?

I have a procedure that performs some calculations on all records returned by a cursor. It looks a bit like this:
PROCEDURE do_calc(id table.id_column%TYPE)
IS
CURSOR c IS
SELECT col1, col2, col3
FROM table
WHERE ...;
BEGIN
FOR r IN c LOOP
-- do some complicated calculations using r.col1, r.col2, r.col3 etc.
END LOOP;
END;
Now I have the case where I need to perform the exact same calculation on a different set of records that come from a different table. However, these have the same "shape" as in the above in example.
Is it possible to write a procedure that looks like this:
PROCEDURE do_calc2(c some_cursor_type)
IS
BEGIN
FOR r IN c LOOP
-- do the calc, knowing we have r.col1, r.col2, r.col3, etc.
END LOOP;
END;
I know about SYS_REFCURSOR, but I was wondering if it was possible to use the much more convenient FOR ... LOOP syntax and implicit record type.
Create a package.
Declare your cursor as package variable.
Use %rowtype to set function parameter type.
create or replace package test is
cursor c is select 1 as one, 2 as two from dual;
procedure test1;
function test2(test_record c%ROWTYPE) return number;
end test;
create or replace package body test is
procedure test1 is
begin
for r in c loop
dbms_output.put_line(test2(r));
end loop;
end;
function test2(test_record c%ROWTYPE) return number is
l_summ number;
begin
l_summ := test_record.one + test_record.two;
return l_summ;
end;
end test;
I had a similar problem, where I had two cursors that needed to be processed the same way, so this is how I figured it out.
DECLARE
--Define our own rowType
TYPE employeeRowType IS RECORD (
f_name VARCHAR2(30),
l_name VARCHAR2(30));
--Define our ref cursor type
--If we didn't need our own rowType, we could have this: RETURN employees%ROWTYPE
TYPE empcurtyp IS REF CURSOR RETURN employeeRowType;
--Processes the cursors
PROCEDURE process_emp_cv (emp_cv IN empcurtyp) IS
person employeeRowType;
BEGIN
LOOP
FETCH emp_cv INTO person;
EXIT WHEN emp_cv%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Name = ' || person.f_name ||
' ' || person.l_name);
END LOOP;
END;
--Defines the cursors
PROCEDURE mainProcedure IS
emp empcurtyp;
BEGIN
OPEN emp FOR SELECT first_name, last_name FROM employees WHERE salary > 50000;
process_emp_cv(emp);
CLOSE emp;
OPEN emp FOR SELECT first_name, last_name FROM kuren WHERE first_name LIKE 'J%';
process_emp_cv(emp);
CLOSE emp;
END;
BEGIN
mainProcedure;
END;
/
You can also use this if you want to bulk collect your cursors. You just need to change your helper procedure process_emp_cv; the rest can stay the same.
Using BULK COLLECT
--Processes the cursors
PROCEDURE process_emp_cv (emp_cv IN empcurtyp) IS
TYPE t_employeeRowTable IS TABLE OF employeeRowType;
employeeTable t_employeeRowTable;
BEGIN
LOOP
FETCH emp_cv BULK COLLECT INTO employeeTable LIMIT 50;
FOR indx IN 1 .. employeeTable.Count
LOOP
DBMS_OUTPUT.PUT_LINE('Name = ' || employeeTable(indx).f_name ||
' ' || employeeTable(indx).l_name);
END LOOP;
EXIT WHEN emp_cv%NOTFOUND;
END LOOP;
END;
Try this one, Usong ref cursor.
declare
type c is ref cursor;
c2 c;
type rec is record(
id number,
name varchar(20)
);
r rec;
procedure p1(c1 in out c,r1 in out rec)is begin
loop
fetch c1 into r1;
exit when c1%notfound;
dbms_output.put_line(r1.id || ' ' ||r1.name);
end loop;
end;
begin
open c2 for select id, name from student;
p1(c2,r);
end;
Yes you can use Cursor explicitly into procedure and function,for that cursor need to declare into package as variable

Resources