Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Show Table or Column in body of email generated by UTL_SMTP (SQL Developer)

adnan645May 18 2020 — edited May 20 2020

Hi,

I have written down complete query that I am using to generate emails from SQL Developer having query results as well in the body of email. It works fine. My question is that it only shows results when the query's output is only one cell. If this is a column or a table, then it gives error. How can we:

  1. show a complete column or table on the body of email; or
  2. otherwise send as email attachment.

Looking at just red part is enough to understand the basic question.

====================================================================================

DECLARE

    V_FROM                VARCHAR2(80) := 'SQL.Hyperion@company.com';

    V_RECIPIENT          VARCHAR2(80) := 'aarshad@company.com';

    V_SUBJECT             VARCHAR2(80) := 'SQL Statisics';

    V_MAIL_HOST           VARCHAR2(30) := 'hostname';

    V_MAIL_CONN           UTL_SMTP.CONNECTION;

    V_MSG_BODY_WORKFLOW   VARCHAR2(5000);

   V_MSG_BODY_DATA       VARCHAR2(5000);

    V_MSG_BODY_ORF        VARCHAR2(5000);

    V_OUTPUT_WORKFLOW     VARCHAR2(5000);

   V_OUTPUT_DATA         VARCHAR2(5000);

    V_OUTPUT_ORF          VARCHAR2(5000);

    CRLF                  VARCHAR2(2) := CHR(13)

                        || CHR(10);

BEGIN

    V_OUTPUT_WORKFLOW := 'select count(*) from email_workflow';

    EXECUTE IMMEDIATE V_OUTPUT_WORKFLOW

    INTO V_MSG_BODY_WORKFLOW;

    V_OUTPUT_DATA := 'select count(*) from email_data';

    EXECUTE IMMEDIATE V_OUTPUT_DATA

    INTO V_MSG_BODY_DATA;

    V_OUTPUT_ORF := 'select * from email_ORF';

    EXECUTE IMMEDIATE V_OUTPUT_ORF

    INTO V_MSG_BODY_ORF;

    V_MAIL_CONN := UTL_SMTP.OPEN_CONNECTION(V_MAIL_HOST, 25);

    UTL_SMTP.HELO(V_MAIL_CONN, V_MAIL_HOST);

    UTL_SMTP.MAIL(V_MAIL_CONN, V_FROM);

    UTL_SMTP.RCPT(V_MAIL_CONN, V_RECIPIENT);

    UTL_SMTP.DATA(V_MAIL_CONN, 'Date: '

                               || TO_CHAR(SYSDATE, 'Dy, DD Mon YYYY hh24:mi:ss')

                               || CRLF

                               || 'From: '

                               || V_FROM

                               || CRLF

                               || 'Subject: '

                               || V_SUBJECT

                               || CRLF

                               || 'To: '

                               || V_RECIPIENT

                               || CRLF

                               || CRLF

                               || 'Statisitics for the day to time....'

                               || CRLF

                               || CRLF

                               || 'Workflow actions:'

                               || CRLF

                               || V_MSG_BODY_WORKFLOW

                               || CRLF

                               || 'Data entries:'

                               || CRLF

                               || V_MSG_BODY_DATA

                               || CRLF

                               || 'New ORFs:'

                               || CRLF

                               || V_MSG_BODY_ORF

                               || CRLF

                               || ''

                               || CRLF

                               || 'Sincerely, '

                               || CRLF

                               || 'Oracle Administrator '

                               || CRLF);

    UTL_SMTP.QUIT(V_MAIL_CONN);

EXCEPTION

    WHEN UTL_SMTP.TRANSIENT_ERROR OR UTL_SMTP.PERMANENT_ERROR THEN

        RAISE_APPLICATION_ERROR(-20000, 'Unable to send mail', TRUE);

END;

This post has been answered by Marwim on May 20 2020
Jump to Answer
Comments
Post Details
Added on May 18 2020
5 comments
1,755 views