Dynamicly Fill Array From Table at PL/SQL? - oracle

i need to pass array values dynamic at this PL/SQL CODE But the Array Just See The First Value the code : Hint( The code is big so i cut it at the issue part)please
if you need more information Kindly Ask me,Here is my try to pass it Dynamic.
Variable fill code:
FOR ET IN (SELECT EMAIL
FROM XX_INTERCO_SYS_CON
WHERE ACC_NO = Q.ACC_NO AND TYPE = 'TO')
LOOP
IF P_TO IS NOT NULL
THEN
P_TO := P_TO || ',''' || ET.EMAIL||'''';
ELSE
P_TO := ''''||ET.EMAIL||'''';
END IF;
END LOOP;
FOR EC IN (SELECT EMAIL
FROM XX_INTERCO_SYS_CON
WHERE ACC_NO = Q.ACC_NO AND TYPE = 'CC')
LOOP
IF P_CC IS NOT NULL
THEN
P_CC := P_CC || ',''' || EC.EMAIL||'''';
ELSE
P_CC := ''''||EC.EMAIL||'''';
END IF;
END LOOP;
The Array fill part :
XX_MAIL_PKG.SEND (P_FROM => P_FROM,
P_TO => XX_MAIL_PKG.ARRAY ( P_TO),
P_CC => XX_MAIL_PKG.ARRAY (P_CC),
P_SUBJECT => 'test',
P_HTML_MSG => P_HTML_OUTPUT,
P_SMTP_HOST => P_SMTP_HOST,
P_SMTP_PORT => P_SMTP_PORT,
P_TEXT_MSG => NULL,
X_ERR_MSG => X_ERR_MSG);
COMMIT;
DBMS_OUTPUT.PUT_LINE (X_ERR_MSG);

You appear to be using collections - so (without knowing what is in the XX_MAIL_PKG package) you could try this:
DECLARE
V_TO XX_MAIL_PKG.ARRAY;
V_CC XX_MAIL_PKG.ARRAY;
BEGIN
SELECT EMAIL
BULK COLLECT INTO V_TO
FROM XX_INTERCO_SYS_CON
WHERE ACC_NO = Q.ACC_NO AND TYPE = 'TO';
SELECT EMAIL
BULK COLLECT INTO V_CC
FROM XX_INTERCO_SYS_CON
WHERE ACC_NO = Q.ACC_NO AND TYPE = 'CC';
XX_MAIL_PKG.SEND (P_FROM => P_FROM,
P_TO => V_TO,
P_CC => V_CC,
P_SUBJECT => 'test',
P_HTML_MSG => P_HTML_OUTPUT,
P_SMTP_HOST => P_SMTP_HOST,
P_SMTP_PORT => P_SMTP_PORT,
P_TEXT_MSG => NULL,
X_ERR_MSG => X_ERR_MSG);
COMMIT;
DBMS_OUTPUT.PUT_LINE (X_ERR_MSG);
END;
/

Related

How to re-design code with UTL_SMTP - Oracle Apex send mail

It's been a hard time not able to make my code to work with UTL_SMTP package.
Can some one guide me to change below code and make it work with UTL_SMTP method
I created a mailing application which sends mail to all user on their email id's with an attachment.
Took reference and I built it same exactly like this
https://blogs.oracle.com/apex/post/creating-email-campaign-app-with-oracle-apex
My Code :
declare
l_context apex_exec.t_context;
l_emailsidx pls_integer;
l_namesids pls_integer;
l_region_id number;
begin
-- Get the region id for the CUSTOMERS IR region
select region_id
into l_region_id
from apex_application_page_regions
where application_id = :APP_ID
and page_id = 1
and static_id = 'CUSTOMERS';
l_context := apex_region.open_query_context (
p_page_id => 1,
p_region_id => l_region_id );
-- Get the column positions for EMAIL and NAME columns
l_emailsidx := apex_exec.get_column_position( l_context, 'EMAIL' );
l_namesids := apex_exec.get_column_position( l_context, 'NAME' );
-- Loop throught the query of the context
while apex_exec.next_row( l_context ) loop
l_id : = apex_mail.send (
p_to => apex_exec.get_varchar2( l_context, l_emailsidx ),
p_template_static_id => 'NEW_YEAR_2020_PROMOTION_DUBAI_BRANCH',
p_placeholders => '{' ||
' "CUSTOMER":' || apex_json.stringify( apex_exec.get_varchar2( l_context, l_namesids )) ||
' ,"START_DATE":' || apex_json.stringify( :P2_START_DATE ) ||
' ,"END_DATE":' || apex_json.stringify( :P2_END_DATE ) ||
' ,"LOCATION":' || apex_json.stringify( :P2_LOCATION ) ||
' ,"NOTES":' || apex_json.stringify( :P2_NOTES ) ||
' ,"ITEMS":' || apex_json.stringify( :P2_ITEMS ) ||
' ,"MY_APPLICATION_LINK":' || apex_json.stringify( apex_mail.get_instance_url || apex_page.get_url( 1 )) ||
'}' );
end loop;
for rec in (
select filename, blob_content, mime_type
from apex_application_files
where flow_id = :APP_ID
and filename = 'apex_logo.png'
)
loop
apex_mail.add_attachment(
p_mail_id => l_id,
p_attachment => rec.blob_content,
p_filename => rec.filename,
p_mime_type => rec.mime_type);
end loop;
apex_mail_queue(
p_smtp_hostname => 'xyz.xmp.it',
p_smtp_portno => 45
)
apex_exec.close( l_context );
exception
when others then
apex_exec.close( l_context );
raise;
end;
Need to convert my above code below like this
UTL_MAIL.send (
sender => 'example#ac.cc',
recipents => 'abcexample#ac.cc',
cc => 'xyzexample#ac.cc',
subject => 'Testing',
mime_type => 'text/html',
message => 'ABC TESTING'
);
My below code does not support at my end. It throws network access denied as (ACL). Only UTL_SMTP is supported and mail is sent. But below code need to transform as above UTL_SMTP.send method
l_id : = apex_mail.send (
p_to => apex_exec.get_varchar2( l_context, l_emailsidx ),
p_template_static_id => 'NEW_YEAR_2020_PROMOTION_DUBAI_BRANCH',
p_placeholders => '{' ||
' "CUSTOMER":' || apex_json.stringify( apex_exec.get_varchar2( l_context, l_namesids )) ||
' ,"START_DATE":' || apex_json.stringify( :P2_START_DATE ) ||
' ,"END_DATE":' || apex_json.stringify( :P2_END_DATE ) ||
' ,"LOCATION":' || apex_json.stringify( :P2_LOCATION ) ||
' ,"NOTES":' || apex_json.stringify( :P2_NOTES ) ||
' ,"ITEMS":' || apex_json.stringify( :P2_ITEMS ) ||
' ,"MY_APPLICATION_LINK":' || apex_json.stringify( apex_mail.get_instance_url || apex_page.get_url( 1 )) ||
'}' );
end loop;
for rec in (
select filename, blob_content, mime_type
from apex_application_files
where flow_id = :APP_ID
and filename = 'apex_logo.png'
)
loop
apex_mail.add_attachment(
p_mail_id => l_id,
p_attachment => rec.blob_content,
p_filename => rec.filename,
p_mime_type => rec.mime_type);
end loop;
apex_mail_queue(
p_smtp_hostname => 'xyz.xmp.it',
p_smtp_portno => 45
)
Following is the YouTube followed to create the application
https://www.youtube.com/watch?v=aZQ04yjS7rQ

Using APEX_DATA_EXPORT to store PDF files in DB

I am trying to figure out how to use APEX_DATA_EXPORT API to store PDF file in database table. I created a table with one column as a blob.
Has anyone tried to use APEX_DATA_EXPORT to store data in database?
DECLARE
l_context apex_exec.t_context;
l_export apex_data_export.t_export;
BEGIN
apex_session.create_session (
p_app_id => 130408,
p_page_id => 1,
p_username => 'EXAMPLE USER' );
l_context := apex_exec.open_query_context(
p_location => apex_exec.c_location_local_db,
p_sql_query => 'select * from emp' );
l_export := apex_data_export.export (
p_context => l_context,
p_format => nvl(:format, apex_data_export.c_format_pdf),
p_file_name => 'employees' );
apex_exec.close( l_context );
apex_data_export.download(
p_export => l_export,
--p_content_disposition => apex_data_export.c_inline,
p_stop_apex_engine => false);
insert into pdf_test4 (pdf) values l_export;
EXCEPTION
when others THEN
apex_exec.close( l_context );
raise;
END;
I managed to get it working for Excel so I guess it would work the same for pdf (change p_format)
DECLARE
v_sql varchar2(32000) := 'select * from my_data_table';
v_sql_trimmed varchar2(32000);
l_query_output CLOB;
p_file_id number;
l_file_name varchar2(200) := 'My File - '||sysdate;
p_status varchar2(100);
p_tag_id number;
l_context apex_exec.t_context;
l_export apex_data_export.t_export;
BEGIN
begin
l_context := apex_exec.open_query_context(
p_location => apex_exec.c_location_local_db,
p_sql_query => v_sql );
l_export := apex_data_export.export (
p_context => l_context,
p_format => apex_data_export.c_format_xlsx,
p_file_name => l_file_name );
apex_exec.close( l_context );
insert into my_files_table columns (file_blob,filename,file_mimetype,TAG,LOG,TAG_ID) values (l_export.content_blob , l_export.file_name,l_export.mime_type,'Latest','Y',1);
EXCEPTION
when others THEN
apex_exec.close( l_context );
raise;
END;
end;

Send SQL query output as CSV attachment (pl/sql)

I have a pl/sql query and I want it's output to be sent in email in CSV format straightaway. I have no directory to first create and save a CSV file and then pick it up to send as an attachment.
Please help with your inputs as I am not able to get away.
Regards,
Sachin
Finally figured out a solution with the help of pointers received and providing the same to further help in case someone else needs in future.
My problem was that I was mostly seeing the examples where i could either save the file on a directory or pick the file from a directory to send as an attchment but I had no provision of directory and I wanted query result to be put in CSV and sent in email dynamically. So here is the complete solution.
CREATE OR REPLACE PROCEDURE SEND_CSV_ATTACHMENT AS
v_sender VARCHAR2(130);
v_recipients VARCHAR2(4000);
v_cc VARCHAR2(4000);
v_bcc VARCHAR2(2000);
v_subj VARCHAR2(200);
v_msg CLOB;
v_mime VARCHAR2(40);
v_tbl VARCHAR2(20000);
c_cr_lf CONSTANT CHAR (2) := (CHR (13) || CHR (10)); -- Carriage Return/Line Feed characters for formatting text emails
v_loop_count PLS_INTEGER := 0;
v_attachment CLOB;
v_block_qry VARCHAR2(3000);
v_block_row VARCHAR2(6000);
TYPE bl_cur IS REF CURSOR;
v_result bl_cur;
v_rowcount NUMBER;
errMsg VARCHAR2(15000);
BEGIN
v_sender := 'somesender#xyzcommunications.com';
SELECT NVL(EMAIL_LIST, 'someone#abcd.com')
FROM
(
SELECT LISTAGG(EMAIL_ID, ',') WITHIN GROUP (ORDER BY EMAIL_ID) AS EMAIL_LIST FROM RECIPEINTS_TABLE WHERE SEND_TO = 1 AND IS_ACTIVE = 1
);
SELECT NVL(EMAIL_LIST, 'someone#abcd.com')
FROM
(
SELECT LISTAGG(EMAIL_ID, ',') WITHIN GROUP (ORDER BY EMAIL_ID) AS EMAIL_LIST FROM RECIPEINTS_TABLE WHERE SEND_CC = 1 AND IS_ACTIVE = 1
);
v_bcc := 'someone#abcd.com';
-- Generate attachment - Begin
v_attachment := '"COL1", "COL2"' || CHR(13) || CHR(10);
v_block_qry := 'SELECT ''"'' || COL1 || ''", "'' || COL2 || ''"'' AS ROWTXT
FROM MY_TABLE';
OPEN v_result FOR v_block_qry;
LOOP
v_rowcount := v_result%ROWCOUNT;
FETCH v_result INTO v_block_row;
EXIT WHEN v_result%NOTFOUND;
v_attachment := v_attachment || v_block_row || chr(13) || chr(10);
END LOOP;
CLOSE v_result;
-- Generate attachment - End
v_subj:= 'MAIL_SUBJECT ' || TO_CHAR(TRUNC(SYSDATE-1), 'YYYY-MM-DD');
UTL_MAIL.send_attach_varchar2(sender => v_sender,
recipients => v_recipients,
cc => v_cc,
bcc => v_bcc,
subject => v_subj,
message => v_msg,
mime_type => 'text/html; charset=us-ascii', -- send html e-mail
attachment => v_attachment,
att_inline => FALSE,
att_filename => 'Change_Report' || TO_CHAR(TRUNC(SYSDATE-1), 'YYYY-MM-DD') || '.csv');
EXCEPTION
WHEN OTHERS THEN
errMsg := SQLERRM;
SEND_MAIL_HTML ('someone#abcd.com', NULL, NULL, errMsg, 'SEND_MAIL ERROR: ' || errMsg);
END SEND_CSV_ATTACHMENT;
You may create such a procedure :
create or replace procedure prFileSend is
v_mail_owner varchar2(100):='myname#someComp.com';
v_url varchar2(4000);
v_rep varchar2(4000);
delimiter varchar2(1) := chr(38);
begin
for c in ( select * from myTable )
loop
begin
v_url := 'http://www.mycompany.com/einfo/default.aspx?email='||c.email || delimiter || 'p1=' || c.col1 || delimiter ||'p2='||c.col2;
v_rep := utl_http.request(utl_url.escape(v_url, false,'ISO-8859-9'));
end;
end loop;
exception
when others then
prErrorMsgSend(v_mail_owner,'Error : ' || sqlerrm); -- a function like this one which sends an error message back to you.
end;
and create a scheduler job
begin
dbms_scheduler.create_job (
job_name => 'jbFileSend ',
job_type => 'STORED_PROCEDURE',
job_action => 'prFileSend',
start_date => '22-jan-2018 09:00:00 am',
repeat_interval => 'FREQ=DAILY; INTERVAL=1',
comments => 'Sending Every day'
enabled => true);
end;
working every day as an example.

merge report:ORA-28138:An invalid policy predicate was specified

When i excute a merge statement:
merge into employees e using new_employees n
on (e.employee_id = n.employee_id)
when matched then
update set e.salary = n.salary
when not matched then
insert (employee_id,first_name,last_name,email,phone_number,hire_date,
job_id,salary,commission_pct,manager_id,department_id)
values (n.employee_id,n.first_name,n.last_name,n.email,n.phone_number,
n.hire_date,n.job_id,n.salary,n.commission_pct,n.manager_id,
n.department_id);
it reports:ORA-28138:An invalid policy predicate was specified.
What's wrong with me?
I find this error is caused by Fine_Grained Auditing.I had created a policy before as below:
DBMS_FGA.add_policy (object_schema => 'primer',
object_name => 'employees',
policy_name => 'audit_policy',
audit_condition => 'employee_id = 100',
audit_column => 'phone_number,salary',
handler_schema => 'primer',
handler_module => 'FGA_SEND_MAIL(primer,employees,audit_policy)',
ENABLE => TRUE,
statement_types => 'select,insert,update,delete',
audit_trail => DBMS_FGA.db_extended,
audit_column_opts => DBMS_FGA.any_columns);
create or replace procedure primer.fga_send_mail (p_schema in varchar2,
p_obj in varchar2,
p_pol in varchar2)
as
begin
dbms_output.put_line ('Audit Trail Generated');
dbms_output.put_line ('SQL=' || sys_context ('USERENV', 'CURRENT_SQL'));
dbms_output.put_line ('USER' || sys_context ('USERENV', 'SESSION_USER'));
end;
What wrong with it?

Job/Procedure to PL/SQL execution?

I've this PL/SQL and is working correctly also email is sended.
BEGIN
FOR cur_rec IN
(select JOB, SCHEMA_USER, WHAT from dba_jobs where Broken = 'Y') LOOP
BEGIN
SCHEMA2.send_mail(
p_to => 'receive#test.com',
p_from => 'send#test.com',
p_subject => 'JOB Report',
p_message => 'Job name is: ' || cur_rec.what,
p_smtp_host => 'webmail.test.com');
END;
END LOOP;
END;
/
I need to schedule that PL/SQL execution every 4 hours, But I don't know how to create a job or a procedure with that code, I've tried a lot but still saying:
Completed with warnings
Any help to construct the job or/and procedure is appreciated.
begin
DBMS_SCHEDULER.CREATE_JOB (
job_name=>'my_job',
job_type=>'PLSQL_BLOCK',
job_action=>
'BEGIN
FOR cur_rec IN
(select JOB, SCHEMA_USER, WHAT from dba_jobs where Broken = ''Y'') LOOP
BEGIN
SCHEMA2.send_mail(
p_to => ''receive#test.com'',
p_from => ''send#test.com'',
p_subject => ''JOB Report'',
p_message => ''Job name is: '' || cur_rec.what,
p_smtp_host => ''webmail.test.com'');
END;
END LOOP;
END;',
start_date=>sysdate+1, --start tomorrow at this time
repeat_interval=>'FREQ=HOURLY; INTERVAL=4', --repeat every 4 hours
auto_drop=>false
);
end;
/
Review documentation for additional options.. create job

Resources