Oracle cursor for dynamic query - oracle

I have an SP that tries to execute a cursor for a dynamic built query
Here is the code
procedure echeancnew(p_entity in varchar2,
ref_data OUT ref_cursor)
as
v_sql VARCHAR2(2000);
v_dblink VARCHAR2(20);
v_entity VARCHAR2(20);
v_test1 VARCHAR2(999);
v_test2 VARCHAR2(999);
begin
SELECT e.dblink,e.descr
into v_dblink, v_entity
FROM CHM_ENTITIES e
WHERE e.abr = p_entity;
v_sql := q'|
SELECT j.ENTRY,
j.Curr "CUR",
j.Ttype "TYPE",
j.third_role "ROLE",
t.Name_i "THIRD",
j.third "No",
t.Country_Descr "COUNTRY",
To_Char(j.expire, 'MONTHYYYY') "M",
Pkg_Jou.Ent_Bal#|' || v_dblink || q'|(Entry) "AMOUNT",
Pkg_Jou.Ent_Bal#|' || v_dblink || q'|(Entry) * j.rate "AMNT CHF",
Sale_Trader.Trader_Descr "RESPV",
Purch_Trader.Trader_Descr "RESPA",
T2.Coface "Assur.",
CASE
WHEN Nvl(TRUNC(SYSDATE) - TRUNC(j.expire), 0) < 15 THEN 'A<15'
WHEN Nvl(TRUNC(SYSDATE) - TRUNC(j.expire), 0) BETWEEN 15 AND 30 THEN 'B>15<30'
WHEN Nvl(TRUNC(SYSDATE) - TRUNC(j.expire), 0) BETWEEN 30 AND 60 THEN 'C>30<60'
WHEN Nvl(TRUNC(SYSDATE) - TRUNC(j.expire), 0) BETWEEN 60 AND 120 THEN 'D>60<120'
ELSE 'E>120'
END "Past Due Inv.",'|'
|| v_entity || q'|' as Entity
FROM Jou_Jou#|' || v_dblink || q'| j
inner join Thr_v_Third#|' || v_dblink || q'| t ON j.Ttype = t.Ttype
AND j.Third = t.Third
inner join Thr_Third#|' || v_dblink || q'| T2 ON j.Ttype = T2.Ttype
AND j.Third = T2.Third
right outer join Tra_Affair#|' || v_dblink || q'| a ON j.Entry = a.Entry_Nr
right outer join Tra_Delivery#|' || v_dblink || q'| d ON a.Delivery = d.Delivery_Id
inner join Tra_Contract#|' || v_dblink || q'| Purch ON d.From_Purchase_Contract = Purch.Contract
inner join Tra_Trader#|' || v_dblink || q'| Purch_Trader ON Purch.Trader_Transact = Purch_Trader.Trader
inner join Tra_Contract#|' || v_dblink || q'| Sale ON d.To_Sale_Contract =Sale.Contract
inner join Tra_Trader#|' || v_dblink || q'| Sale_Trader ON Sale.Trader_Transact = Sale_Trader.Trader
WHERE j.Curr IN ('USD', 'EUR', 'GBP', 'CHF')
AND Pkg_Jou.Ent_Bal#|' || v_dblink || q'|(Entry) != 0;|';
v_test1 := substr(v_sql, 1 , 950);
v_test2 := substr(v_sql, 951, 950);
OPEN ref_data FOR v_sql; -- HERE I GET THE ERROR
end echeancnew;
The error that I get is
ORA-00911: invalid character
When I concat v_test1 and v_test2 and run it as select it works
What I am doing wrong?

If I were you, I'd remove empty lines, but - what certainly is wrong - is semi-colon that terminates the V_SQL statement:
AND Pkg_Jou.Ent_Bal#|' || v_dblink || q'|(Entry) != 0;|';
^
|
remove it!

Related

Not able to execute with clause in dynamic sql

I'm trying to execute below statement using dynamic SQL but I'm getting this exception
"ORA-00911:
invalid character on ORA-06512".
When i remove semicolon(;) the query runs but it's taking a long time and is not able to display output.
How do I clear this problem? Is anywhere I'm doing wrong? if so please guide me.
Below is my code:
DECLARE
mapping_rule VARCHAR2 (10000)
:= 'SALES_REVIEW.NET_VALUE-SALES_REVIEW.GROSS_VALUE+SALES_REVIEW_TABLE.PLNT';
v_mapp_rule VARCHAR2 (10000);
v_mapp_rule_1 VARCHAR2 (10000);
v_chk_flag CHAR (1) := 'Y';
v_mapping_rule VARCHAR2 (10000);
v_str VARCHAR2 (30000);
BEGIN
<<dest_stmt>>
DBMS_OUTPUT.put_line (v_chk_flag);
IF v_chk_flag = 'Y'
THEN
v_mapping_rule := mapping_rule;
ELSE
v_mapping_rule := v_mapp_rule_1;
END IF;
v_str :=
'WITH sel_col (rowno, mapp_rule)
AS ( SELECT ROWNUM rowno,
REGEXP_SUBSTR ( ''' || v_mapping_rule
|| ''',''([+--*!#/#$%^&()=<>,?]+|[A-Z0-9_.'''']+|\s+)'',
1,
LEVEL)
mapp_rule
FROM DUAL
CONNECT BY REGEXP_SUBSTR ('''
|| v_mapping_rule
|| ''',''([+--*!#/#$%^&()=<>,?]+|[A-Z0-9_.'''']+|\s+)'',
1,
LEVEL)
IS NOT NULL)
SELECT listagg (
CASE WHEN B.MAPP_RULE IS NOT NULL THEN B.MAPP_RULE
ELSE D.MAPP_RULE END,
'''')
WITHIN GROUP (ORDER BY d.rowno)
FROM RRR_PROCESS_DTLS a
JOIN RRR_PROCESS_MAPPING_DTLS b
ON A.PROCESS_ID = B.PROCESS_ID
AND A.COMPANY_ID = B.COMPANY_ID
JOIN RRR_DEST_TABLE_DTLS C
ON A.DEST_TABLE = C.TABLE_ID
AND A.COMPANY_ID = C.COMPANY_ID
RIGHT JOIN sel_col d
ON CONCAT (CONCAT (C.TABLE_NAME, ''.''), B.DEST_COLUMN) =
d.mapp_rule
AND A.PROCESS_ID = 12
AND A.COMPANY_ID = 2
ORDER BY d.rowno;';
DBMS_OUTPUT.put_line (v_str);
EXECUTE IMMEDIATE v_str into v_mapp_rule_1 ;
DBMS_OUTPUT.put_line ('bf ' || v_mapp_rule_1);
IF v_mapp_rule_1 IS NOT NULL
THEN
v_chk_flag := 'N';
GOTO dest_stmt;
END IF;
DBMS_OUTPUT.put_line (v_mapp_rule_1);
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.PUT_LINE (
SQLCODE
|| ' -ERROR- '
|| SQLERRM
|| ' on '
|| DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
END;

Porting from Oracle to Postgres

Earlier our hosted environments were on Oracle but now we have recently switched to postgres. Porting the tables I believe was successfully since it has been working correct for a long time. I have been actually struggling with porting a procedure but is not working for some reason. I went through the documentation part of the ora2pg but I am not able to crack, which I believe is the last piece of the puzzle.
I've started with this one, which looks like this in Oracle:
create or replace
procedure c_audit(anonymous in boolean, aud_level IN varchar)
AUTHID CURRENT_USER IS
script varchar2(32000);
acc_select varchar(100);
open_cursor integer;
returnval integer;
p_id integer;
a_id integer;
p_name varchar2(100);
a_name varchar2(100);
v_count integer;
c_count integer;
doc_count integer;
curr_user varchar(100);
begin
for i in (select a.a_name a_name, a.a_id, p.name p_name, p.id p_id, ds.username username
from c_account a
inner join c_pro p on a.a_id = p.a_id
inner join c_dat_ds_xref x on p.id = x.p_id
inner join c_data ds on x.id_datasource = ds.id
inner join c_conntypes ct on x.id_conntype = ct.id_conntype
where ct.typeid = 'CAPTURE'
order by a.a_name, p.name)
LOOP
curr_user := i.username;
IF anonymous = true
THEN
acc_select := 'select ' || '''' || i.a_id || '''' || ' a_id,' || '''' || i.p_id || '''' || ' p_id';
ELSE
acc_select := 'select ' || '''' || i.a_name || '''' || ' a_name,' || '''' || i.p_name || '''' || ' p_name';
END IF;
IF upper(aud_level) = 'VERBATIM'
THEN
script:= acc_select || '
, count(distinct d.document_id) docCount
, sum(case when v.document_id is null or v.verbatim_type_value = ''NO_VERBATIM_TEXT'' then 0 else 1 end) VerbCount
, sum(case when v.document_id is null then (select to_number(prop_value) verbSize
from c_properties
where prop_name = ''METERING.STRUCT.ONLY.CHARGE''
and id_pro = 0) else v.credits end) CreditCount
from ' || i.username || '.p_document d
left outer join (
select vi.document_id, t.verbatim_type_value
, case when dbms_lob.substr(vi.extracted_original,8) = ''<cbnull>''
or t.verbatim_type_value = ''NO_VERBATIM_TEXT''
then coalesce(s2.strucCredit, .25)
else ceil(vi.extracted_original_size/coalesce(s.verbSize, 2048)) end credits
from ' || i.username || '.p_verbatim vi
left outer join ' || i.username || '.pd_verbatim_type t on vi.verbatim_type_id = t.verbatim_type_id
, (
select to_number(prop_value) verbSize
from c_properties
where prop_name = ''METERING.MAXSIZE.VERBATIM''
and id_pro = 0
) s
, (
select to_number(prop_value) strucCredit
from c_properties
where prop_name = ''METERING.STRUCT.ONLY.CHARGE''
and id_pro = 0
) s2
) v on d.document_id = v.document_id';
ELSE
IF upper(aud_level) = 'DOCUMENT'
THEN
script:= acc_select || '
, count(distinct a.document_id) docCount
, sum(credits) creditCount
from (
select d.document_id, ceil(sum(v.extracted_original_size)/coalesce(s.verbSize,2048)) credits
from ' || i.username || '.p_document d
inner join ' || i.username || '.p_verbatim v on d.document_id = v.document_id
inner join ' || i.username || '.pd_verbatim_type t on v.verbatim_type_id = t.verbatim_type_id
, (
select to_number(prop_value) verbSize
from c_properties
where prop_name = ''METERING.MAXSIZE.VERBATIM''
and id_pro = 0
) s
where t.verbatim_type_value <> ''NO_VERBATIM_TEXT''
and dbms_lob.substr(v.extracted_original,8) <> ''<cbnull>''
group by d.document_id, s.verbSize
union
select d.document_id, coalesce(s2.strucCredit, .25)
from ' || i.username || '.p_document d
, (
select to_number(prop_value) strucCredit
from c_properties
where prop_name = ''METERING.STRUCT.ONLY.CHARGE''
and id_pro = 0
) s2
where d.document_id not in (select distinct v.document_id from ' || i.username || '.p_verbatim v)
union
select distinct d.document_id, coalesce(s2.strucCredit, .25)
from ' || i.username || '.p_document d
inner join ' || i.username || '.p_verbatim v on d.document_id = v.document_id
inner join ' || i.username || '.pd_verbatim_type t on v.verbatim_type_id = t.verbatim_type_id
, (
select to_number(prop_value) strucCredit
from c_properties
where prop_name = ''METERING.STRUCT.ONLY.CHARGE''
and id_pro = 0
) s2
where (t.verbatim_type_value = ''NO_VERBATIM_TEXT''
or dbms_lob.substr(v.extracted_original,8) = ''<cbnull>'')
) a';
ELSE
dbms_output.put_line('Invalid choice for audit level, no audit generated');
exit;
END IF;
END IF;
begin
open_cursor := dbms_sql.open_cursor;
DBMS_SQL.PARSE(open_cursor, script,
DBMS_SQL.NATIVE);
IF anonymous = true then
dbms_sql.define_column(open_cursor,1,a_id);
dbms_sql.define_column(open_cursor,2,p_id);
else
dbms_sql.define_column(open_cursor,1,a_name,100);
dbms_sql.define_column(open_cursor,2,p_name,100);
end if;
dbms_sql.define_column(open_cursor,3,doc_count);
dbms_sql.define_column(open_cursor,4,v_count);
IF upper(aud_level) = 'VERBATIM' then
dbms_sql.define_column(open_cursor,5,c_count);
end if;
returnval := DBMS_SQL.EXECUTE(open_cursor);
loop
if dbms_sql.fetch_rows(open_cursor) > 0 then
IF anonymous = true then
dbms_sql.column_value(open_cursor,1,a_id);
dbms_sql.column_value(open_cursor,2,p_id);
dbms_sql.column_value(open_cursor,3,doc_count);
dbms_sql.column_value(open_cursor,4,v_count);
IF upper(aud_level) = 'VERBATIM' then
dbms_sql.column_value(open_cursor,5,c_count);
dbms_output.put_line(a_id || ',' || p_id || ',' || doc_count || ',' || v_count || ',' || c_count);
else
dbms_output.put_line(a_id || ',' || p_id || ',' || doc_count || ',' || v_count);
end if;
else
dbms_sql.column_value(open_cursor,1,a_name);
dbms_sql.column_value(open_cursor,2,p_name);
dbms_sql.column_value(open_cursor,3,doc_count);
dbms_sql.column_value(open_cursor,4,v_count);
IF upper(aud_level) = 'VERBATIM' then
dbms_sql.column_value(open_cursor,5,c_count);
dbms_output.put_line(a_name || ',' || p_name || ',' || doc_count || ',' || v_count || ',' || c_count);
else
dbms_output.put_line(a_name || ',' || p_name || ',' || doc_count || ',' || v_count);
end if;
end if;
else
exit;
end if;
end loop;
exception
when others then
--dbms_output.put_line('Error occured. Please check if the current user has Select access to table ' || curr_user || '.p_document ' || curr_user || '.p_verbatim ' || curr_user || '.pd_verbatim_type');
dbms_output.put_line('Error occured. Please login as ' || curr_user || ' and run the following:');
dbms_output.put_line('GRANT SELECT ON ' || curr_user || '.P_DOCUMENT to ' || user ||';');
dbms_output.put_line('GRANT SELECT ON ' || curr_user || '.P_VERBATIM to ' || user ||';');
dbms_output.put_line('GRANT SELECT ON ' || curr_user || '.pd_verbatim_type to ' || user ||';');
end;
end loop;
end;
Does this procedure appears correct with respect to syntax?
CREATE OR REPLACE FUNCTION c_audit(anonymous boolean, aud_level text) RETURNS VOID AS $body$
DECLARE
script text;
acc_select text;
returnval integer;
p_id integer;
a_id integer;
i record;
p_name text;
a_name text;
v_count integer;
c_count integer;
doc_count integer;
curr_user text;
BEGIN
for i in (SELECT a.a_name a_name, a.a_id, p.name p_name, p.id p_id, ds.username username
from c_account a
inner join c_pro p on a.a_id = p.a_id
inner join c_dat_ds_xref x on p.id = x.p_id
inner join c_data ds on x.id_datasource = ds.id
inner join c_conntypes ct on x.id_conntype = ct.id_conntype
where ct.typeid = 'CAPTURE'
order by a.a_name, p.name)
LOOP
curr_user := i.username;
IF anonymous = true
THEN
acc_select := 'SELECT ' || '''' || i.a_id || '''' || ' AccountID,' || '''' || i.p_id || '''' || ' ProjectID';
ELSE
acc_select := 'SELECT ' || '''' || i.a_name || '''' || ' AccountName,' || '''' || i.p_name || '''' || ' ProjectName';
END IF;
IF upper(aud_level) = 'VERBATIM'
THEN
script:= acc_select || '
, count(distinct d.document_id) docCount
, sum(case when coalesce(CAST(v.document_id AS text), '') = '' or v.verbatim_type_value = ''NO_VERBATIM_TEXT'' then 0 else 1 end) VerbCount
, sum(case when coalesce(CAST(v.document_id AS text), '') = '' then (SELECT to_number(prop_value,''9999.99'') verbSize
from c_properties
where prop_name = ''METERING.STRUCT.ONLY.CHARGE''
and id_project = 0) else v.credits end) CreditCount
from ' || i.username || '.p_document d
left outer join (
SELECT vi.document_id, t.verbatim_type_value
, case when substr(vi.extracted_original,8) = ''<cbnull>''
or t.verbatim_type_value = ''NO_VERBATIM_TEXT''
then coalesce(s2.strucCredit, .25)
else ceil(vi.extracted_original_size/coalesce(s.verbSize, 2048)) end credits
from ' || i.username || '.p_verbatim vi
left outer join ' || i.username || '.pd_verbatim_type t on vi.verbatim_type_id = t.verbatim_type_id
, (
select to_number(prop_value,''9999.99'') verbSize
from c_properties
where prop_name = ''METERING.MAXSIZE.VERBATIM''
and id_project = 0
) s
, (
select to_number(prop_value,''9999.99'') strucCredit
from c_properties
where prop_name = ''METERING.STRUCT.ONLY.CHARGE''
and id_project = 0
) s2
) v on d.document_id = v.document_id';
SELECT format(script) into script;
ELSE IF upper(aud_level) = 'DOCUMENT'
THEN
script:= acc_select || '
, count(distinct a.document_id) docCount
, sum(credits) creditCount
from (
SELECT d.document_id, ceil(sum(v.extracted_original_size)/coalesce(s.verbSize,2048)) credits
from ' || i.username || '.p_document d
inner join ' || i.username || '.p_verbatim v on d.document_id = v.document_id
inner join ' || i.username || '.pd_verbatim_type t on v.verbatim_type_id = t.verbatim_type_id
, (
SELECT to_number(prop_value,''9999.99'') verbSize
from c_properties
where prop_name = ''METERING.MAXSIZE.VERBATIM''
and id_project = 0
) s
where t.verbatim_type_value <> ''NO_VERBATIM_TEXT''
and substr(v.extracted_original,8) <> ''<cbnull>''
group by d.document_id, s.verbSize
union
select d.document_id, coalesce(s2.strucCredit, .25)
from ' || i.username || '.p_document d
, (
select to_number(prop_value,''9999.99'') strucCredit
from c_properties
where prop_name = ''METERING.STRUCT.ONLY.CHARGE''
and id_project = 0
) s2
where d.document_id not in (select distinct v.document_id from ' || i.username || '.p_verbatim v)
union
select distinct d.document_id, coalesce(s2.strucCredit, .25)
from ' || i.username || '.p_document d
inner join ' || i.username || '.p_verbatim v on d.document_id = v.document_id
inner join ' || i.username || '.pd_verbatim_type t on v.verbatim_type_id = t.verbatim_type_id
, (
select to_number(prop_value,''9999.99'') strucCredit
from c_properties
where prop_name = ''METERING.STRUCT.ONLY.CHARGE''
and id_project = 0
) s2
where (t.verbatim_type_value = ''NO_VERBATIM_TEXT''
or substr(v.extracted_original,8) = ''<cbnull>'')
) a';
SELECT format(script) into script;
ELSE
SELECT format(script) into script;
exit;
END IF;
END IF;
BEGIN
IF anonymous = true
THEN
IF upper(aud_level) = 'VERBATIM'
THEN
EXECUTE script into a_id, p_id, doc_count, v_count, c_count;
ELSE
EXECUTE script into a_id, p_id, doc_count, c_count;
END IF;
ELSE
IF upper(aud_level) = 'VERBATIM'
THEN
EXECUTE script into a_name, p_name, doc_count, v_count, c_count;
ELSE
EXECUTE script into a_name, p_name, doc_count, c_count;
END IF;
END IF;
GET DIAGNOSTICS returnval := ROW_COUNT;
LOOP
IF returnval > 0
THEN
IF anonymous = true
THEN
IF upper(aud_level) = 'VERBATIM'
THEN
SELECT format ('Information %s, %s, %s, %s, %s', a_id, p_id, doc_count, v_count, c_count);
ELSE
SELECT format ('Information %s, %s, %s, %s', a_id, p_id, doc_count, c_count);
END IF;
ELSE
IF upper(aud_level) = 'VERBATIM'
THEN
SELECT format ('Information %s, %s, %s, %s, %s', a_name, p_name, doc_count, v_count, c_count);
ELSE
SELECT format ('Information %s, %s, %s, %s', a_name, p_name, doc_count, c_count);
END IF;
END IF;
ELSE
EXIT;
END IF;
END LOOP;
EXCEPTION
WHEN others THEN
PERFORM format('Error occured. Please login as %s, %s' , curr_user , ' and run the following:');
PERFORM format('GRANT SELECT ON %s.P_DOCUMENT to %s', curr_user, user);
PERFORM format('GRANT SELECT ON %s.P_VERBATIM to %s', curr_user, user);
PERFORM format('GRANT SELECT ON %s.pd_verbatim_type to %s', curr_user, user);
END;
END LOOP;
END;
$body$
LANGUAGE PLPGSQL
;
ALTER FUNCTION cb_audit(boolean, text) OWNER TO USER;
-- REVOKE ALL ON FUNCTION cb_audit FROM PUBLIC;
The error for failure I get is -
ERROR: too many parameters specified for RAISE
Where: PL/pgSQL function "cb_audit" line 145 at RAISE
I found this to be a good link which I used as a reference
I believe that for porting DBMS_OUTPUT.PUT_LINE, RAISE NOTICE should be the right way. I had encountered another error which was for format of the - to_number(prop_value,'9999.99') which appears right as per the syntax mentioned here but then for some reason when I switched to to_number(prop_value,''9999.99''), I did not get the error but not sure why that should be or even if it should work correctly.
The version of Postgres -
PostgreSQL 9.1.10 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-3), 64-bit
Edited:
I actually attempted to modify this function based on the suggestions from Patrick but due to some reason it is not displaying anything on the screen. I added format() at the end of every script to display the script as but it just executed the code and displays c_audit and null. Though I execute the individual subsql and they do return expected counts and results. Am I missing anything?
You are using two types of string "building": the concatenation || operator and the % placeholder. Both are perfectly legal, but use of the format() function is preferred because the code is cleaner and PG guards against things like SQL-injection behind the scenes.
The error you are getting is that one of your RAISE NOTICE commands has a number of % different from the arguments you supply; forgive me for not looking that up but line 145 is hard to find here on SO. In general, you should rewrite all of them like so:
RAISE NOTICE format('GRANT SELECT ON %I.pd_verbatim_type to %I', curr_user, user);
The %I placeholder takes a SQL identifier as input: it can not be NULL and it will be properly quoted to avoid SQL-injection and keyword collisions. (From your code I take it that curr_user is a schema name and user is a role name, both SQL identifiers.)
Note also that the PERFORM statement in a PL/pgSQL function is a SELECT statement that does not return data, but it is executed to examine a side effect, such as asserting that some data exists. As a consequence, there is no PERFORM privilege, use GRANT SELECT instead.
When quoting in PostgreSQL, SQL-identifiers use double quotes, while string literal values use single quotes. The to_number() function definitely requires single quotes.
A few more points to improve your code:
(1) The two sub-selects in the dynamic query like this one...
'select to_number(prop_value,''9999.99'') strucCredit
from c_properties
where prop_name = ''METERING.STRUCT.ONLY.CHARGE''
and id_project = 0'
... are STABLE: you always get the same result. Instead of leaving them in the dynamic SQL, create two variables and put the results in them before you do anything else:
DECLARE
...
verbSize numeric;
strucCredit numeric;
BEGIN
SELECT to_number(prop_value,'9999.99') INTO verbSize
FROM c_properties
WHERE prop_name = 'METERING.MAXSIZE.VERBATIM' AND id_pro = 0;
SELECT to_number(prop_value,'9999.99') INTO strucCredit
FROM c_properties
WHERE prop_name = 'METERING.STRUCT.ONLY.CHARGE' AND id_pro = 0;
...
Then use the varibale verbSize and strucCredit in your dynamic SQL. Like this you do these queries just once, instead of a few times for every iteration.
(2) All of the dynamic queries need a GROUP BY 1, 2 clause.
(3) The clause CASE WHEN coalesce(CAST(v.document_id AS text), '') = '' ... should be written like CASE WHEN v.document_id IS NULL ..., assuming that v.document_id can not be an empty string.
(4) You changed your RAISE NOTICE statements to SELECT format(...). That latter form produces no output, use RAISE NOTICE format(...) instead.
(5) Rewrite your dynamic SQL to use the format() function too.

ORA-06512:(PL/SQL: SQL Statement ignored)

Whenever I'm trying to execute my stored procedure I got error I have doubt in my third parameter FULL_FILE_NAME(K_XVL_10704_20151703_003.000) because I contains dot(.).How can I pass third parameter.Please let me know if I'm doing wrong somewhere.
CREATE OR REPLACE PROCEDURE DE_DUP_PROC2 (Dy_File_Name IN VARCHAR2,
SUPPLIER_CD IN VARCHAR2,
FULL_FILE_NAME IN VARCHAR2)
Declare
ORGNIZATION_ID VARCHAR2(20);
v_sql4 VARCHAR2(200);
DE_DUP_COUNT NUMBER(38);
BEGIN
SELECT ORG_ID INTO ORGNIZATION_ID FROM ps_org WHERE ORG_SHORT_NM = SUPPLIER_CD;
v_sql3 :=
' SELECT count(*) FROM (SELECT stg.*, row_number() over (partition BY key_clmns_hash ORDER BY 1) AS RN FROM '
|| Dy_File_Name || ' stg ) s JOIN ps_pharmacy p ON s.extrnl_pharmacy_id = p.extrnl_pharmacy_id LEFT JOIN ps_rx_hist H ON h.key_clmns_hash = s.key_clmnS_hash
AND h.rx_dspnsd_dt = s.rx_dspnsd_dt AND s.supplier_pharmacy_cd = h.SUPPLIER_PHARMACY_CD WHERE S.RN > 1 OR s.detl_clmns_hash = h.detl_clmns_hash ' ;
EXECUTE IMMEDIATE v_sql3 INTO DE_DUP_COUNT;
IF DE_DUP_COUNT > 0
THEN
v_sql4 :=
' declare
SRC_ID NUMBER(38) := SRC_FILE_ID_SEQ.nextval;
begin
insert into PS_FILE ( SRC_FILE_ID,FILE_NM,FILE_PROC_DT ,ORG_ID) values(SRC_ID,' || FULL_FILE_NAME || ', SYSDATE , ' || ORGNIZATION_ID || ' );
commit;
FOR i in ( '
|| ' SELECT S.TRANS_GUID AS OLD_TRANS_GUID,S.DETL_CLMNS_HASH AS DETL_CLMNS_HASH1 ,S.KEY_CLMNS_HASH AS KEY_CLMNS_HASH1,S.RX_DSPNSD_DT AS R_DSPNSD_DT,
S.SUPPLIER_PHARMACY_CD AS SUPPLIER_PHARMACY_CD1 FROM (SELECT stg.*, row_number() over (partition BY key_clmns_hash ORDER BY 1) AS RN FROM
'
|| Dy_File_Name || ' stg ) s JOIN ps_pharmacy p ON s.extrnl_pharmacy_id = p.extrnl_pharmacy_id LEFT JOIN ps_rx_hist H
ON h.key_clmns_hash = s.key_clmnS_hash
AND h.rx_dspnsd_dt = s.rx_dspnsd_dt
AND s.supplier_pharmacy_cd = h.SUPPLIER_PHARMACY_CD
WHERE S.RN > 1
OR s.detl_clmns_hash = h.detl_clmns_hash ' || ')' || ' LOOP ' || ' ' || '
insert into PS_RX_DUPES2(TRANS_GUID,DETL_CLMNS_HASH,KEY_CLMNS_HASH,RX_DSPNSD_DT,SUPPLIER_PHARMACY_CD,SRC_FILE_ID)
values(i.OLD_TRANS_GUID,i.DETL_CLMNS_HASH1,i.KEY_CLMNS_HASH1,i.R_DSPNSD_DT,i.SUPPLIER_PHARMACY_CD1,SRC_ID);
commit; ' || ' END LOOP;' || ' END;';
dbms_output.put_line(v_sql4);
EXECUTE IMMEDIATE v_sql4;
END IF;
END DE_DUP_PROC2;
When I'm executing I got below error:
declare
OUTPUT_STATUS number(2);
begin
DE_DUP_PROC2('10655_20150318_04119297','MCL','K_XVL_10704_20151703_003.000');
end;
Error at line 1
ORA-06550: line 2, column 118:
PL/SQL: ORA-00917: missing comma
ORA-06550: line 2, column 16:
PL/SQL: SQL Statement ignored
ORA-06512: at "PS_ADMIN.DE_DUP_PROC2", line 4
ORA-06512: at line 2
First about the procedure cannot be compiled - there should be IS/AS instead of DECLARE and you miss declaration of v_sql3 variable.
Secondly, if I assume that FULL_FILE_NAME and ORGANIZATION_ID are values and not column names these must be enclosed with single quotes like
'... values(SRC_ID,''' || FULL_FILE_NAME || ''', SYSDATE , ''' || ORGNIZATION_ID || ''' )'.
Also the length of the v_sql4 is too small.
And most likely more after that.

I'm getting this error when ever I'm executing my stored procedure " PLS-00103"

I'm trying to execute stored procedure, I'm getting stuck in 54 line 'EXECUTE IMMEDIATE' basically. I'm passing SQL statement into for loop.Please let me know I'm doing wrong somewhere?
CREATE OR REPLACE Procedure DE_DUP_PROC (Dy_File_Name in USER_TABLES.table_name%type,
SUPPLIER_CD in varchar2,
EXT_PHARMA_ID in varchar2,
FLAG_VALUE in varchar2,
DE_REC_COUNT out NUMBER) --RETURN NUMBER
AS
SEQ_NO_SHO Number(38); --EEEE
WYYYYNNN VARCHAR2(250) := 'W2015021';
YYYYNNN VARCHAR2(10);
CUR_DATE Date;
--De_Rec_Count Number(38) := 3456;
DE_DUB_OUTPUT_FILE VARCHAR2(100);
/*CURSOR De_DUB_CUR IS */
DE_DUB_SQL_STATMNT VARCHAR2(3000) := 'SELECT S.TRANS_GUID AS OLD_TRANS_GUID,
H.TRANS_GUID AS NEW_TRANS_GUID,
CASE
WHEN H.TRANS_GUID IS NULL
THEN 0
ELSE 1
END as TRN_STAT,P.INTR_PHARMACY_ID as INT_PHARMACY_ID ,S.EXTRNL_PHARMACY_ID as EXT_PHARMACY_ID ,S.PHARMACY_NM as PHARMACY_NAME ,S.PHARMACY_ADDR as PHARMACY_ADDRESS,
S.SUPPLIERS_PSCR_DRUG_CD as SP_PSCR_DRUG_CD, S.PSCR_DRUG_IPU_CD as PS_DRUG_IPU_CD,''IPU'' as IPU_Value, S.PSCR_DRUG_DESC as PS_DRUG_DESC,
S.DSPNSD_DRUG_PACK_SIZE as DS_DRUG_PACK_SIZE, S.RX_ID as R_ID, S.RX_ITEM_SEQ as R_ITEM_SEQ, S.RX_REPEAT_STATUS as R_REPEAT_STATUS, S.RX_TYP as R_TYPE,
S.EXMT_STATUS as EX_STATUS,S.PSCR_QTY as PS_QTY, S.NRSG_HM_IND as NR_HM_IND,S.RX_DSPNSD_DT as R_DSPNSD_DT, S.RX_DSPNSD_TM as R_DSPNSD_TM,
S.SUPPLIERS_DSPNSD_DRUG_CD as SP_DSPNSD_DRUG_CD, S.DSPNSD_DRUG_IPU_CD as DS_DRUG_IPU_CD, ''IPU'' as IPU_Value2,S.DSPNSD_DRUG_DESC as DS_DRUG_DESC,
S.GENERIC_USE_MARKER as GC_USE_MARKER, S.DSPNSD_UNIT_OF_QTY as DS_UNIT_OF_QTY, S.DSPNSD_QTY as DS_QTY, ''EUR'' as EUR_Value1,S.COST_OF_DSPNSD_QTY as CT_OF_DSPNSD_QTY ,
S.VERBOSE_DOSAGE as VER_DOSAGE FROM
(SELECT stg.*, row_number() over (partition BY key_clmns_hash ORDER BY 1) AS RN FROM '|| Dy_File_Name ||' stg ) s
LEFT JOIN ps_pharmacy p
ON s.extrnl_pharmacy_id = p.extrnl_pharmacy_id
LEFT JOIN ps_rx_hist H
ON h.key_clmns_hash = s.key_clmnS_hash
AND h.rx_dspnsd_dt = s.rx_dspnsd_dt
AND s.supplier_pharmacy_cd = h.SUPPLIER_PHARMACY_CD
AND s.detl_clmns_hash <> h.detl_clmns_hash
WHERE s.RN = 1';
BEGIN
EXECUTE IMMEDIATE 'SELECT count(*) into DE_REC_COUNT
FROM (SELECT stg.*, row_number() over ( partition BY key_clmns_hash ORDER BY 1 ) AS RN FROM '|| Dy_File_Name ||' stg ) s
LEFT JOIN ps_pharmacy p ON s.extrnl_pharmacy_id = p.extrnl_pharmacy_id LEFT JOIN ps_rx_hist H ON h.key_clmns_hash = s.key_clmnS_hash
AND h.rx_dspnsd_dt = s.rx_dspnsd_dt AND s.supplier_pharmacy_cd = h.SUPPLIER_PHARMACY_CD AND s.detl_clmns_hash <> h.detl_clmns_hash WHERE S.RN = 1';
IF DE_REC_COUNT > 0 THEN
--select sysdate into CUR_DATE from dual;
--select PROC_PD_CD into WYYYYNNN from PS_ADMIN.PS_PROC_PD where PD_STRT_DT <= CURRENT_DATE and PD_END_DT >= CURRENT_DATE;
--select PROC_PD_CD into WYYYYNNN from PS_ADMIN.PS_PROC_PD where PD_STRT_DT <= CURRENT_DATE and PD_END_DT >= CURRENT_DATE; -- PD_STRT_DT<='16-AUG-15' and PD_STRT_DT >= '16-AUG-15';
select replace(WYYYYNNN,'W','') into YYYYNNN from dual;
SELECT PS_GET_PROC_PD(SUPPLIER_CD,EXT_PHARMA_ID,YYYYNNN) into SEQ_NO_SHO FROM DUAL;
select 'LRXIE'||FLAG_VALUE||'10_'||SUPPLIER_CD||'_'||EXT_PHARMA_ID||'_'||WYYYYNNN||'_'||SEQ_NO_SHO||'_'||DE_REC_COUNT||'.TXT' into DE_DUB_OUTPUT_FILE from dual;
--DBMS_OUTPUT.PUT_LINE( De_Dub_Output_File );
FOR De_Dub_rec IN EXECUTE IMMEDIATE DE_DUB_SQL_STATMNT
LOOP
dbms_output.enable(100000);
DBMS_OUTPUT.PUT_LINE ('"' || De_Dub_rec.OLD_TRANS_GUID || '"|"' || De_Dub_rec.NEW_TRANS_GUID || '"|' || De_Dub_rec.TRN_STAT || '|' || De_Dub_rec.INT_PHARMACY_ID || '|' || De_Dub_rec.EXT_PHARMACY_ID || '|"' ||
De_Dub_rec.PHARMACY_NAME|| '"|"' || De_Dub_rec.PHARMACY_ADDRESS || '"|' || De_Dub_rec.SP_PSCR_DRUG_CD || '|' || De_Dub_rec.PS_DRUG_IPU_CD || '|"' || De_Dub_rec.IPU_Value || '"|"' ||
De_Dub_rec.PS_DRUG_DESC || '"|' || De_Dub_rec.DS_DRUG_PACK_SIZE || '|"' || De_Dub_rec.R_ID || '"|' || De_Dub_rec.R_ITEM_SEQ || '|' || De_Dub_rec.R_REPEAT_STATUS || '|"' ||
De_Dub_rec.R_TYPE || '"|"' || De_Dub_rec.EX_STATUS || '"|' || De_Dub_rec.PS_QTY || '|' || De_Dub_rec.NR_HM_IND || '|"' || De_Dub_rec.R_DSPNSD_DT || '"|' || De_Dub_rec.R_DSPNSD_TM || '|' ||
De_Dub_rec.SP_DSPNSD_DRUG_CD || '|' || De_Dub_rec.DS_DRUG_IPU_CD|| '|"' || De_Dub_rec.IPU_Value2 || '"|"' || De_Dub_rec.DS_DRUG_DESC|| '"|' || De_Dub_rec.GC_USE_MARKER|| '|"' ||
De_Dub_rec.DS_UNIT_OF_QTY|| '"|' || De_Dub_rec.DS_QTY|| '|"' || De_Dub_rec.EUR_Value1|| '"|' || De_Dub_rec.CT_OF_DSPNSD_QTY || '|"' || De_Dub_rec.VER_DOSAGE || '"');
END LOOP;
DE_REC_COUNT :=0;
ELSE
DE_REC_COUNT :=1;
END IF;
END DE_DUP_PROC;
/
I'm getting this error:
LINE/COL ERROR
-------- -----------------------------------------------------------------
58/44 PLS-00103: Encountered the symbol "IMMEDIATE" when expecting one
of the following:
. ( * # % & - + / at loop mod remainder rem ..
<an exponent (**)> || multiset
The symbol ". was inserted before "IMMEDIATE" to continue.
There are two errors in your code. The first one is the compiler error you get. You can't use an execute immediate in a for loop (which is clearly documented in the manual)
You need to open a cursor and then loop over the cursor. So instead of
FOR De_Dub_rec IN EXECUTE IMMEDIATE DE_DUB_SQL_STATMNT
you need to use something like this:
OPEN De_Dub_cursor FOR DE_DUB_SQL_STATMNT;
LOOP
FETCH De_Dub_cursor INTO de_dub_cursor_record;
EXIT WHEN cv%NOTFOUND;
... do your stuff here
END LOOP;
Of course you will need to declare the cursor De_Dub_cursor and the record variable de_dub_cursor_record. Note that the record variables needs to be defined with all columns that your result returns (which essentially requires a new TYPE to be defined if I'm not mistaken)
The second error you have won't show up until you run the code. You have an INTO variable clause inside your SQL string for the first EXECUTE IMMEDIATE. This will not work. The into clause can not be used like that. You need to remove the into DE_REC_COUNT part from the string literal and use the INTO clause as an option to the execute immediate statement. Something like this:
EXECUTE IMMEDIATE 'SELECT count(*) FROM ....'
INTO DE_REC_COUNT;
Unrelated to your problems, but the select ... from dual can be replaced with a simple assignment.
So instead of
select replace(WYYYYNNN,'W','') into YYYYNNN from dual;
use
YYYYNNN := replace(WYYYYNNN,'W','');
or instead of:
SELECT PS_GET_PROC_PD(SUPPLIER_CD,EXT_PHARMA_ID,YYYYNNN) into SEQ_NO_SHO FROM DUAL;
select 'LRXIE'||FLAG_VALUE||'10_'||SUPPLIER_CD||'_'||EXT_PHARMA_ID||'_'||WYYYYNNN||'_'||SEQ_NO_SHO||'_'||DE_REC_COUNT||'.TXT' into DE_DUB_OUTPUT_FILE from dual;
use
SEQ_NO_SHO := PS_GET_PROC_PD(SUPPLIER_CD,EXT_PHARMA_ID,YYYYNNN);
DE_DUB_OUTPUT_FILE := 'LRXIE'||FLAG_VALUE||'10_'||SUPPLIER_CD||'_'||EXT_PHARMA_ID||'_'||WYYYYNNN||'_'||SEQ_NO_SHO||'_'||DE_REC_COUNT||'.TXT';
First, should be execute immediate 'select count(*) from ...' into DE_REC_COUNT;
That's what I see at first scan, but the fail is at second execute immediate.
There you should create a collection and execute immediate the query and bulk collect into that collection.
Then, you should loop in that collection and do the work that you do(the dbms_output stuff).

Getting ORA-01747: invalid user.table.column, table.column, or column specification in Oracle when inserting data using a dynamic insert

Here is the code.
p_id number,
p_mc varchar2
Both of the above parameters will be passed in during a procedure call.
EXECUTE IMMEDIATE 'INSERT INTO COUNT_MASTER_TEMP ' || 'SELECT COUNT (ar.'|| p_mc || ')' ||
'
FROM app_recipient ar
WHERE EXISTS (SELECT r.' || p_mc ||
' FROM app_recipient r
WHERE r.ID =' || p_id || ' AND ar.'|| p_mc || '= r.'|| p_mc ||')';
My stored procedure is as follows:
input parameters are p_id (which is a numeric value), p_mc is a actually a column name. sample data is p_id = 6372325 and p_mc can be MC1, MC2 till MC14.
CREATE OR REPLACE PROCEDURE HCP_360.sp_get_all_records
(p_id NUMBER,
p_mc varchar2,
p_detail OUT SYS_REFCURSOR)
IS
v_count NUMBER;
v_master_id NUMBER;
v_sql VARCHAR2(2000);
--v_sql2 VARCHAR2(2000);
v_mc VARCHAR2(255):=p_mc;
cur_detail SYS_REFCURSOR;
BEGIN
BEGIN
EXECUTE IMMEDIATE 'DELETE FROM COUNT_MASTER_TEMP';
execute immediate 'INSERT INTO COUNT_MASTER_TEMP ' ||
'SELECT COUNT (ar.'|| v_mc|| ')' || '
FROM app_recipient ar
WHERE EXISTS (SELECT r.' || v_mc || ' FROM app_recipient r
WHERE r.ID =' || p_id || ' AND ar.'|| v_mc || '= r.'|| v_mc||')';
END;
BEGIN
select c_count
into v_count
from COUNT_MASTER_TEMP;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
v_count := 0;
END;
BEGIN
IF v_count > 0
THEN
v_sql := 'SELECT master_id
FROM app_recipient
WHERE ' || v_mc || ' IN (SELECT r.'||v_mc ||
' FROM app_recipient r
WHERE r.ID = ' || p_id || ')
AND ROWNUM <= 1
AND master_id IS NOT NULL
ORDER BY master_id DESC';
EXECUTE IMMEDIATE 'DELETE FROM COUNT_MASTER_TEMP';
EXECUTE IMMEDIATE 'INSERT INTO COUNT_MASTER_TEMP ' || v_sql;
END IF;
select c_count
into v_master_id
from COUNT_MASTER_TEMP;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
v_master_id := 0;
END;
BEGIN
v_sql :=
'
SELECT r.ID,
r.master_id,
v.RECIPIENT_STATUS,
v.PARENT_OR_CHILD,
nvl(v.CHILD_COUNT, 0) CHILD_COUNT,
r.IS_PICKABLE,
r.IS_GOLDEN,
r.request_wf_state,
r.record_type,
r.first_name,
r.last_name,
r.'||v_mc ||
',r.middle,
r.title,
r.name_of_organization,
r.name_of_business,
r.address,
r.city,
r.state,
r.country,
v.HCP_TYPE,
v.HCP_SUBTYPE,
v.is_edit_locked,
v.record_type as rec_type,
v.DATA_SOURCE_NAME,
v.DEA_DATA,
v.NPI_DATA,
v.STATE_DATA,
RPPS,
v.finess,
v.siren_number
FROM app_recipient r
left join V_MASTER_RECIP_W_TRXN_OP v
on r.id = v.id
or r.master_id =' || v_master_id||
'WHERE' ||'r.'||v_mc || '= ANY
(SELECT ar.'||v_mc || 'FROM app_recipient ar WHERE r.ID =' || p_id || ')
UNION ALL
SELECT r.ID,
r.master_id,
v.RECIPIENT_STATUS,
v.PARENT_OR_CHILD,
nvl(v.CHILD_COUNT, 0) CHILD_COUNT,
r.IS_PICKABLE,
r.IS_GOLDEN,
r.request_wf_state,
r.record_type,
r.first_name,
r.last_name,
r.'||v_mc ||
',r.middle,
r.title,
r.name_of_organization,
r.name_of_business,
r.address,
r.city,
r.state,
r.country,
v.HCP_TYPE,
v.HCP_SUBTYPE,
v.is_edit_locked,
v.record_type as rec_type,
v.DATA_SOURCE_NAME,
v.DEA_DATA,
v.NPI_DATA,
v.STATE_DATA,
RPPS,
v.finess,
v.siren_number
FROM app_recipient r
left join V_MASTER_RECIP_W_TRXN_OP2 v
on r.id = v.id
or r.master_id =' || v_master_id ||'
WHERE r.'||v_mc ||'= ANY (SELECT ar.'||v_mc ||'FROM app_recipient ar WHERE r.ID =' ||v_master_id || ')';
open cur_detail for v_sql;
p_detail := cur_detail;
end;
END;
/
if you use pid as a VARCHAR with non-numeric data.. and you have embed it in single quotes, during query formation, or define it as a bind variable.
Always use bind variables where-ever possible.
EXECUTE IMMEDIATE
'INSERT INTO COUNT_MASTER_TEMP ' ||
'SELECT COUNT (ar.'|| p_mc || ')' ||
' FROM app_recipient ar ' ||
' WHERE EXISTS (SELECT r.' || p_mc ||
' FROM app_recipient r ' ||
' WHERE r.ID = :pid AND ar.'|| p_mc || '= r.'|| p_mc||')'
using pid;
You could test the string formation using DBMS_OUTPUT.
For example,
SQL> set serveroutput on
SQL> DECLARE
2 p_mc VARCHAR2(20);
3 p_id NUMBER;
4 BEGIN
5 p_mc := 'mc';
6 p_id := 1;
7 dbms_output.put_line( 'INSERT INTO COUNT_MASTER_TEMP ' ||
8 'SELECT COUNT (ar.'|| p_mc || ')' || '
9 FROM app_recipient ar
10 WHERE EXISTS (SELECT r.' || p_mc || ' FROM app_recipient r
11 WHERE r.ID =' || p_id || ' AND ar.'|| p_mc || '= r.'|| p_mc||')');
12 END;
13 /
INSERT INTO COUNT_MASTER_TEMP SELECT COUNT (ar.mc)
FROM app_recipient ar
WHERE
EXISTS (SELECT r.mc FROM app_recipient r
WHERE r.ID =1 AND ar.mc= r.mc)
PL/SQL procedure successfully completed.
SQL>
Always remember to first test the dynamic string using DBMS_OUTPUT. Once you are confirm that the string is correctly formed, remove/comment the dbms_output and use EXECUTE IMMEDIATE.
Update Thanks to Alex Poole. The actual issue is here:
'WHERE' ||'r.'||v_mc || '= ANY
After WHERE, there is no space, thus the query fails. You need to add a space after WHERE.
'WHERE ' ||'r.'||v_mc || '= ANY
The only way I can immediately see to get that error from insert is if you're passing the column name enclosed in single-quotes; you haven't shown the call and it's unlikely you'd do this from a SQL client (more likely you added quotes incorrectly in a JDBC/PHP/etc. parameter), but calling like this:
exec sp_get_all_records(6372325, '''MC1''', :detail);
would try to execute generated SQL that looks like this:
INSERT INTO COUNT_MASTER_TEMP SELECT COUNT (ar.'MC1')
FROM app_recipient ar
WHERE EXISTS (SELECT r.'MC1' FROM app_recipient r
WHERE r.ID =6372325 AND ar.'MC1'= r.'MC1')
with the column name starting with the illegal single-quote character, and that would indeed throw:
ORA-01747: invalid user.table.column, table.column, or column specification
ORA-06512: at "SCHEMA.SP_GET_ALL_RECORDS", line 17
ORA-06512: at line 1
and line 17 is the execute immediate 'INSERT....
You are also missing some whitespace in your final v_sql construct, which will case the open cur_detail to get an "ORA-00933: SQL command not properly ended" with what you've shown. You need to change
'WHERE' ||'r.'||v_mc || '= ANY
(SELECT ar.'||v_mc || 'FROM app_recipient ar WHERE r.ID =' || p_id || ')
to
' WHERE ' ||'r.'||v_mc || '= ANY
(SELECT ar.'||v_mc || ' FROM app_recipient ar WHERE r.ID =' || p_id || ')
with spaces ether side of the first WHERE, and before the FROM; and also in the second half of the union change
WHERE r.'||v_mc ||'= ANY (SELECT ar.'||v_mc ||'FROM app_recipient ar WHERE r.ID =' ||v_master_id || ')';
to
WHERE r.'||v_mc ||'= ANY (SELECT ar.'||v_mc ||' FROM app_recipient ar WHERE r.ID =' ||v_master_id || ')';
again adding a space before FROM.
Those won't get the ORA-01747 error you reported, but neither will the insert you showed, which seems to be valid with the arguments you are passing - unless you are incorrectly quoting the column name argument as shown above.
I was testing my procedure using PL/SQL developer. I still do not know why ORA-01747 was occurring. But I modified my procedure and wrote a PL/SQL code for testing the output.
Procedure is as follows:
CREATE OR REPLACE PROCEDURE HCP_360.sp_get_all_records
(p_id NUMBER,
p_mc in varchar2,
p_detail OUT SYS_REFCURSOR,
p_count OUT NUMBER,
p_error OUT NVARCHAR2)
IS
v_count NUMBER;
v_master_id NUMBER;
v_sql LONG;
--v_sql2 VARCHAR2(2000);
v_mc VARCHAR2(255):=p_mc;
cur_detail SYS_REFCURSOR;
BEGIN
BEGIN
-- v_sql := '(SELECT COUNT ( ' ||v_mc || ')' ||
-- '
-- FROM app_recipient
-- WHERE ' ||v_mc || ' IN (SELECT ' || v_mc ||
-- ' FROM app_recipient r
-- WHERE r.ID =' || p_id || ' AND '|| v_mc || '=' || v_mc ||'))';
v_sql :='(SELECT ar.'||v_mc || ' as v_count' ||
'
FROM app_recipient ar
WHERE EXISTS (SELECT r.'||v_mc ||
' FROM app_recipient r
WHERE r.ID =' || p_id || ' AND ar.'||v_mc || '= r.'||v_mc ||'))';
--EXECUTE IMMEDIATE 'DELETE FROM COUNT_MASTER_TEMP';
EXECUTE IMMEDIATE ' select nvl(count(*),0) as count from (' || v_SQL || ') '
INTO v_count;
END;
BEGIN
IF v_count > 0
THEN
v_sql := 'SELECT master_id
FROM app_recipient
WHERE ' || v_mc || ' IN (SELECT r.'||v_mc ||
' FROM app_recipient r
WHERE r.ID = ' || p_id || ')
AND ROWNUM <= 1
AND master_id IS NOT NULL
ORDER BY master_id DESC';
EXECUTE IMMEDIATE ' select nvl(master_id, 0) from (' || v_SQL || ') '
INTO v_master_id;
END IF;
END;
BEGIN
EXECUTE IMMEDIATE ' delete from GTT_RECIPEINTS_FOR_MC';
execute immediate 'insert into GTT_RECIPEINTS_FOR_MC
( MATCH_CODE_VALUE ,master_id ,id)
(SELECT r.'||v_mc ||', r.master_id, r.id
FROM app_recipient r
WHERE r.'||v_mc || '= ANY (SELECT r.'||v_mc || '
FROM app_recipient r
WHERE r.ID =' || p_id||')
UNION ALL
SELECT r.'||v_mc ||', r.master_id, r.id
FROM app_recipient r
WHERE r.ID =' ||v_master_id ||')';
end;
BEGIN
v_sql :=
'
select ( r.id) M_ID, r.RECIPIENT_STATUS, PARENT_OR_CHILD, nvl(CHILD_COUNT,0), r.IS_PICKABLE, r.IS_GOLDEN, r.ID, nvl(r.MASTER_ID,r.id) as MASTER_ID , r.request_wf_state,
r.TITLE, r.FIRST_NAME, r.MIDDLE, r.LAST_NAME,r.FULL_NAME_LNF, r.FULL_NAME_FNF, r.NAME_OF_ORGANIZATION,r.ADDRESS,
r.CITY, r.STATE, r.COUNTRY, r.HCP_TYPE, r.HCP_SUBTYPE, r.is_edit_locked, r.record_type as rec_type,
DATA_SOURCE_NAME,DEA_DATA,NPI_DATA,STATE_DATA,RPPS ,finess,siren_number from v_master_recip_w_trxn_op r join GTT_RECIPEINTS_FOR_MC p on r.id=p.id or r.id= p.master_id
order by id';
open cur_detail for v_sql;
p_detail := cur_detail;
p_count :=SQL%ROWCOUNT;
EXCEPTION
WHEN OTHERS
THEN
p_count := 0;
ROLLBACK;
end;
END;
/
And the PL/SQL code for testing is as follows:
SET SERVEROUTPUT ON SIZE 1000000
DECLARE
l_cursor SYS_REFCURSOR;
p_count number;
p_error NVARCHAR2 (255);
M_ID app_recipient.id%type ;
l_ID app_recipient.id%type ;
l_master_id app_recipient.master_id%type ;
l_RECIPIENT_STATUS V_MASTER_RECIP_W_TRXN_OP2.recipient_status%type;
l_PARENT_OR_CHILD V_MASTER_RECIP_W_TRXN_OP2.parent_or_child%type;
l_CHILD_COUNT V_MASTER_RECIP_W_TRXN_OP2.child_count%type;
l_IS_PICKABLE app_recipient.is_pickable%type;
l_IS_GOLDEN app_recipient.is_golden%type;
l_request_wf_state app_recipient.request_wf_state%type;
l_record_type app_recipient.record_type%type;
l_first_name app_recipient.first_name%type;
l_last_name app_recipient.last_name%type;
l_p_mc app_recipient.MC14%type;
l_middle app_recipient.middle%type;
l_title app_recipient.title%type;
l_name_of_organization app_recipient.name_of_organization%type;
l_name_of_business app_recipient.name_of_business%type;
l_address app_recipient.address%type;
l_city app_recipient.city%type;
l_state app_recipient.state%type;
l_country app_recipient.country%type;
l_HCP_TYPE v_master_recip_w_trxn_op.HCP_TYPE%type;
l_HCP_SUBTYPE V_MASTER_RECIP_W_TRXN_OP.HCP_SUBTYPE%type;
l_is_edit_locked V_MASTER_RECIP_W_TRXN_OP.is_edit_locked%type;
l_rec_type V_MASTER_RECIP_W_TRXN_OP.record_type%type;
l_DATA_SOURCE_NAME V_MASTER_RECIP_W_TRXN_OP.data_source_name%type;
l_DEA_DATA V_MASTER_RECIP_W_TRXN_OP.dea_data%type;
l_NPI_DATA V_MASTER_RECIP_W_TRXN_OP.npi_data%type;
l_STATE_DATA V_MASTER_RECIP_W_TRXN_OP.state_data%type;
l_RPPS V_MASTER_RECIP_W_TRXN_OP.rpps%type;
l_finess V_MASTER_RECIP_W_TRXN_OP.finess%type;
l_siren_number V_MASTER_RECIP_W_TRXN_OP.siren_number%type;
l_FULL_NAME_LNF V_MASTER_RECIP_W_TRXN_OP.siren_number%type;
l_FULL_NAME_FNF V_MASTER_RECIP_W_TRXN_OP.siren_number%type;
BEGIN
hcp_360.hcp360_application.get_recipients_for_mc(p_id => &&var,
p_mc => &&p_mc,
p_detail => l_cursor,
p_count => p_count,
p_error => p_error);
LOOP
FETCH l_cursor
INTO M_ID ,
l_RECIPIENT_STATUS ,
l_PARENT_OR_CHILD ,
l_CHILD_COUNT ,
l_IS_PICKABLE,
l_IS_GOLDEN,
l_ID,
L_MASTER_ID,
l_request_wf_state,
l_title,
l_first_name,
l_middle,
l_last_name,
l_FULL_NAME_LNF,
l_FULL_NAME_FNF,
l_name_of_organization,
l_address,
l_city,
l_state,
l_country,
l_HCP_TYPE,
l_HCP_SUBTYPE,
l_is_edit_locked,
l_rec_type,
l_DATA_SOURCE_NAME,
l_DEA_DATA,
l_NPI_DATA,
l_STATE_DATA,
l_RPPS,
l_finess,
l_siren_number;
EXIT WHEN l_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(M_ID || ' | ' ||
l_RECIPIENT_STATUS || ' | ' ||
l_PARENT_OR_CHILD || ' | ' ||
l_CHILD_COUNT || ' | ' ||
l_IS_PICKABLE || ' | ' ||
l_IS_GOLDEN || ' | ' ||
l_ID || ' | ' ||
L_MASTER_ID || ' | ' ||
l_request_wf_state || ' | ' ||
l_title || ' | ' ||
l_first_name || ' | ' ||
l_middle || ' | ' ||
l_last_name || ' | ' ||
l_FULL_NAME_LNF || ' | ' ||
l_FULL_NAME_FNF || ' | ' ||
l_name_of_organization || ' | ' ||
l_address || ' | ' ||
l_city || ' | ' ||
l_state || ' | ' ||
l_country || ' | ' ||
l_HCP_TYPE || ' | ' ||
l_HCP_SUBTYPE || ' | ' ||
l_is_edit_locked || ' | ' ||
l_rec_type || ' | ' ||
l_DATA_SOURCE_NAME || ' | ' ||
l_DEA_DATA || ' | ' ||
l_NPI_DATA || ' | ' ||
l_STATE_DATA || ' | ' ||
l_RPPS || ' | ' ||
l_finess || ' | ' ||
l_siren_number
);
END LOOP;
CLOSE l_cursor;
END;

Resources