Link to home
Start Free TrialLog in
Avatar of DBL9SSG
DBL9SSGFlag for United States of America

asked on

ORACLE XML From SELECT statement

Hello Experts,

I have a table; ErrTbl with 4 columns:  Process, Num, Desc , StartTime.

What I like to do, is send out an email and have the table results in a nice format.  In sql server, I'm able to pull the results with the following query:

SELECT
td =[Process], '',
td =[Num],   '',
td =[Desc],   '',
td =[StartTime],   '',
td =[StartTime],   ''      
FROM ErrTbl            
FOR XML PATH('tr'), TYPE
                                    
I would like to get similar results using Oracle 10g.  I've played around with DBMS_XMLQuery, but I'm not getting the results that I'm looking for.

Any suggestions are greatly appreciated.

Thank you
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Some of use don't know sqlserver.  Please post sample data and expected results.
Avatar of DBL9SSG

ASKER

I'm attaching a screen shot of what I would like to do.
EmailRslt.JPG
by using SQL Plus:

set markup HTML on
spool index.html
select * from <tablename>;
spool off
set markup HTML off
With the sqlserver code above and that screenshot it looks like a simple html table not really xml.

A picture of the expected results doesn't help much since it depends on the display tool.

Can you provide the specific xml/html you want from an Oracle query for those rows in a table?
Avatar of DBL9SSG

ASKER

My plan is to send out an email, and here's the body for the email:  

Let me know if this is what you're asking for.

'<H1><FONT FACE="Geneva, Arial" SIZE=3>An Error Has Occurred</FONT></H2><table border="1"><th>ProcessName</th><th>ProcessStep</th><th>ProcessStepName</th><th>StartTime</th><th>ProcessStatus</th><tr><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>usp_Import_ISF_Files_Main</FONT></TD><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>1</FONT></TD><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>1.0 Loading E2K Data</FONT></TD><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>2011-03-08T08:00:00.977</FONT></TD></tr><tr><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>usp_Import_ISF_Files_Main</FONT></TD><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>2</FONT></TD><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>2.0 Executing usp_FTPGet_Files</FONT></TD><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>2011-03-08T08:54:12.607</FONT></TD></tr><tr><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>usp_Import_ISF_Files_Main</FONT></TD><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>3</FONT></TD><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>2.5 Truncate Production Tables</FONT></TD><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>2011-03-08T08:54:50.020</FONT></TD></tr><tr><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>usp_Import_ISF_Files_Main</FONT></TD><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>4</FONT></TD><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>3.0 Executing usp_ImportTableData @ImpDirectory = D:\ISM_Temp\ISF_Input</FONT></TD><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>2011-03-08T08:54:50.367</FONT></TD></tr><tr><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>usp_Import_ISF_Files_Main</FONT></TD><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>5</FONT></TD><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>4.0 Executing usp_ISF_users_IMP</FONT></TD><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>2011-03-08T08:59:29.480</FONT></TD></tr><tr><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>usp_Import_ISF_Files_Main</FONT></TD><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>6</FONT></TD><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>5.0 Executing usp_Export_Files D:\ISM_Temp\ISF_Output</FONT></TD><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>2011-03-08T09:01:18.537</FONT></TD></tr><tr><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>usp_Import_ISF_Files_Main</FONT></TD><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>7</FONT></TD><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>6.0 Executing usp_FTPPut_Files</FONT></TD><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>2011-03-08T09:02:09.810</FONT></TD></tr><tr><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>usp_Import_ISF_Files_Main</FONT></TD><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>8</FONT></TD><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>7.0 Executing ups_Create_WK_ISF_Tbls</FONT></TD><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>2011-03-08T09:02:44.240</FONT></TD></tr><tr><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>usp_Import_ISF_Files_Main</FONT></TD><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>9</FONT></TD><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>8.0 Executing ups_Create_WK_ISF_Tbls_BOL</FONT></TD><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>2011-03-08T09:04:08.680</FONT></TD></tr><tr><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>usp_Import_ISF_Files_Main</FONT></TD><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>10</FONT></TD><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>Completion Successfully</FONT></TD><td nowrap><FONT FACE="Geneva, Arial" SIZE=1>2011-03-08T09:05:24.573</FONT></TD></tr></table><br/>';
apart from my simple approach above, you can do advance formatting on the html codes based on this link:

http://download.oracle.com/docs/cd/B19306_01/server.102/b14357/ch7.htm
SOLUTION
Avatar of OP_Zaharin
OP_Zaharin
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Aside from sqlplus, Oracle has several very powerful methods of generating XML.

It can generate just about any variation you need.  If the sqlplus/cmdline approach is what you need then the link above will probably give you what you need.

If you need to issue a select statement in some other tool/platform/??? and need a single select to generate the table, please let us know the tool/platform/??? you want to use.

For example:  PL/SQL stored procedures can send an email directly from the database.

Again, not a sqlserver person are you saying the initially provided simple select statement will generate the html table you posted complete with headings?  
>>you need to write Oracle procedure to send emails:

If you want them to use that approach then you need a solution that does not use sqlplus that you suggested in the previous posts.
Avatar of DBL9SSG

ASKER

We currently have a package that uses UTL_MAIL.SEND to send out an email.  Within that proc, we supply the message, which is the body of the email, and this is where I'd like to place XML.

I just ran the sqlplus/cmdline approach and the results are EXACTLY what I'm looking for:  the results from SELECT * FROM Table look perfect.  However, can I get that result into the message body to send via UTL_Mail?

 
yes - i agree with you.
i'm not clear with the question. he is asking for XML yet showing sample of HTML. well - i added the solution to generate an email via Oracle pl/sql :)

cheers.
OP
DBL9SSG,
glad that the HMTL code in sql plus helps.

this is a great document of sending email using UTL_Mail
http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28419/u_mail.htm

first you need to grant the user on UTL_MAIL : grant execute on utl_mail to <user>;
Avatar of DBL9SSG

ASKER

OP Zaharin,
I just realized that I'm typing XML, but I'm thinking HTML.  My apologies, its been a long day.  

You are correct, I'm giving you HTML earlier.  
No worries DBL9SSG :)
hope the Oracle documentation helps
You realize that utl_mail.send is limited to a 32K message body.  If you need more than that you need to use UTL_SMTP.

I've never tried to simulate the sqlplus HTML reporting in pl/sql and cannot find a quick link that says how.

If no other Expert can provide the solution, I'll see what I can come up with when I get back to work tomorrow.
Avatar of DBL9SSG

ASKER

Thank you all for your inputs, but I have another quertion for OP Zaharin:
The link you sent me to Generate HTML Reports, can I generate the HTML from within PL/SQL and assign that HTML to a LONG variable ?


SlightWV, thanks for the heads up on 32K limit.  I'll investigate on using UTL_SMTP.  I'll certaintly wait to see if you can find anything on HTML reporting in PL/SQL.

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DBL9SSG

ASKER

Slight-
Got your code, basically, we'll have to generate the HTML ourselves?  Oracle doesn't have a way of doing it more simple?


I'm going to accept multiple solutions and close this out, but if you think of anything else, drop me a line at my personal email: <email removed by slightwv>.


Fellas, you guys are Awesome and thank you again for your input.
Yes, agree with slightwv.
Use CLOB, you can store more data than Long datatype.
Avatar of DBL9SSG

ASKER

awesome job!
Oracle and sqlserver compete head to head and typically can do anything the other one can.

Are you saying that you can issues the original select statement posted as-is to generate an html table and all you need to do is issue a simple email call in a stored procedure to get the output you posted?

If so, I'm a little impressed with sqlserver.
Avatar of DBL9SSG

ASKER

SLightwv-

Here's a piece that we use on sqlserver:




      DECLARE @tableHTML  NVARCHAR(MAX) ;

      SET @tableHTML =
            N'<H1><FONT FACE="Geneva, Arial" SIZE=3>An Error Has Occurred</FONT></H2>' +
            N'<table border="1">' +
            N'<th>COLUMNNAME1</th>' +
            CAST (
                        (
                              SELECT
                              td =COLUMNAME1,   ''
                              FROM SQLSERVERTABLE
                              FOR XML PATH('tr'), TYPE
                        ) AS NVARCHAR(MAX)
                  ) + N'</table>'  +
                        N'<br/>'
      
      --Disable Wrapping In Table            
      SELECT @tableHTML = REPLACE(@tableHTML,'<td','<td nowrap')
      
      --Change font style and size
      SELECT @tableHTML = REPLACE(@tableHTML,'<td nowrap>','<td nowrap><FONT FACE="Geneva, Arial" SIZE=1>')
      SELECT @tableHTML = REPLACE(@tableHTML,'</TD','</FONT></TD')



Finally, we use @TableHTML to send on message body.

We can use something similiar from what you sent me last, so I greatly appreciate pointing me the right direction.
Looks like you are basically generating the html manually as well.  That actually makes me feel better.  Still not that much difference between the two.  Its pretty much the same thing I posted.