Skip to Main Content
  • Questions
  • Export query result to Oracle Server

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Abhishek.

Asked: September 01, 2023 - 7:12 am UTC

Last updated: September 06, 2023 - 4:21 am UTC

Version: Version 21.4.2.018

Viewed 100+ times

You Asked

I have written PL/SQL code to store XML into cursor. Refer the below code

Requirement: As of line at line 83, I am inserting the result into another table (XML_GEN) as XMLTYPE datatype. Line: insert into xml_gen (fxdoc) values (i.FXML); But I don't want to that, I need this result to get stored as file on Oracle DB Server. (each row to be stored as a file .xml) I have searched on google, but I am unable to do it.

Can someone provide suggestion or guidance, how I can achieve it.

I have attached the file TableDetail.xls (export of data) and PL/SQL Code I am using.

Below code and table are available at below link as well https://drive.google.com/drive/folders/1d7VPlY0ooR8jr3Xrvd1la1BiEIwFrEhv?usp=drive_link

Code:

declare
  cursor c_fdoc is SELECT id, 
        XMLElement("SDPSyncMessage", 
                  XMLAttributes('http://www.emeter.com/energyip/syncinterface/v8' AS "xmlns"),
                  XMLElement("header",
                    XMLElement("verb",'SDPSync'),
                    XMLElement("noun",'SDPSync'),
                    XMLElement("revision",'1'),
                    XMLElement("dateTime",SYSTIMESTAMP),
                    XMLElement("source",'CIS'),
                    XMLForest(loc_badge "messageID"),
                    XMLElement("syncMode",'sync'),
                    XMLElement("optimizationLevel",'Optimistic') ),                  
                
                XMLElement("payload",
                    XMLElement("servicePoint",
                      XMLForest(loc_badge "mRID"),
                      XMLElement("type",'ServiceDeliveryPoint'),
                      XMLElement("serviceType",'Electric'),
                      XMLElement("className",'Electric'),
                      XMLElement("status",'Active'),
                      XMLElement("premise",
                      XMLForest(phx_loc "mRID")),
                      XMLElement("servicePointStatus",
                      XMLElement("powerStatus",'Y'),
                        CASE WHEN TO_DATE IS NOT NULL THEN XMLElement("loadStatus",'N')
                        ELSE XMLElement("loadStatus",'Y')END,
                      XMLElement("billingHoldStatus",'N'))),
                
                  XMLElement("device",
                      XMLForest(phx_badge "mRID", modeltype "model"),
                      XMLElement("className",'Electric'),
                      XMLElement("status",'Installed'),
                      XMLElement("type",'Meter'),
                      XMLElement("deviceFunctionType",'N'),
                      XMLElement("parameter",
                        XMLElement("name",'Configuration ID'),
                        XMLForest(CONFIGURATIONID "value", EFFECTIVE_DATE "startDate")),
                      XMLElement("parameter",
                        XMLElement("name",'Program ID'),
                        XMLForest(PROGRAMID "value", EFFECTIVE_DATE  "startDate"))),
                 
                  XMLElement("device",
                      XMLForest(phx_badge "mRID", modeltype "commTechnology"),
                      XMLElement("type",'CommModule'),
                      XMLElement("deviceFunctionType",'Y'),
                      XMLElement("className",'Electric'),
                      XMLElement("status",'Active')),
                
                  XMLElement("serviceLocation",
                      XMLForest(phx_loc "mRID"),
                      XMLElement("stateOrProvince",'Arizona'),
                      XMLElement("timeZone",'America/Phoenix'),
                      XMLElement("country",'United States'),
                      XMLElement("status",'Active')),
                 
                  XMLElement("servicePointDeviceAssociation",
                    XMLForest(INSTALL_DATE "startDate"),
                        CASE WHEN TO_DATE IS NOT NULL THEN XMLForest(TO_DATE "endDate") END,
                    XMLElement("servicePointId",
                        XMLForest(loc_badge "mRID")),
                    XMLElement("deviceId",
                        XMLForest(phx_badge "mRID")),
                        XMLElement("relType",'SDP-METER')),
                  
                  XMLElement("deviceFunctionAssociation",
                    XMLForest(INSTALL_DATE "startDate"),
                        CASE WHEN TO_DATE IS NOT NULL THEN XMLForest(TO_DATE "endDate")END,
                    XMLElement("deviceId",
                        XMLForest(phx_badge "mRID")),
                    XMLElement("comFunctionId",
                        XMLForest(phx_badge "mRID")),
                    XMLElement("relType",'COMMUNICATION-METER'))
              ))  AS "FXML" 
  FROM   ASSETS_COPY 
  WHERE FILE_CREATED IS NULL;
begin
  for i in c_fdoc
  loop
    if (i.FXML is not null) then
            --NEED TO UPDATE BELOW LINE OF CODE TO GENERATE XML DIRECTLY TO ORACLE SERVER DIRECTORY
                insert into xml_gen (fxdoc) values (i.FXML);
                update ASSETS_COPY set FILE_CREATED = 'YES' where id = i.id;
                update ASSETS_COPY set FILE_DT = systimestamp where id = i.id;                
            ELSE
                update ASSETS_COPY set FILE_CREATED = 'NO' where id = i.id;
        end if;
  end loop;
end; 


Table:
RTUID | RTUNAME | LOC_BADGE | PHX_LOC | PHX_BADGE | NAME | DEVICE | INSTALL_DATE | EFFECTIVE_DATE | TO_DATE | RECORD_TYPE | RATE | INSTALLED | PROGRAMID | ISRCDC | MODELTYPE | CONFIGURATIONID | FILE_CREATED | FILE_DT | FLEXSYNC | FLEXSYNC_DT | FILE_NAME | LAST_UPDATE_TIME | ID | ERROR | REMOVE | REMOVE_DT
2131931 | 1828329 | 771520006/1828329 | 771520006 | 1828329 | AMS | 2131931 | 02-NOV-16 06.47.43.000000000 AM AMERICA/PHOENIX | 09-JUN-20 12.00.00.000000000 AM AMERICA/PHOENIX | 29-JUN-21 09.24.02.000000000 AM AMERICA/PHOENIX | HISTORY | 24 | 1 | 05 | 1 | 5252B | 001 | | | | | | | 1784292 | | |
2131931 | 1828329 | 771520006/1828329 | 771520006 | 1828329 | AMS | 2131931 | 02-NOV-16 06.47.43.000000000 AM AMERICA/PHOENIX | 29-JUN-21 12.00.00.000000000 AM AMERICA/PHOENIX | 21-JUL-21 08.32.37.000000000 AM AMERICA/PHOENIX | HISTORY | 23 | 1 | 03 | 1 | 5252B | 001 | | | | | | | 1784293 | | |
2131931 | 1828329 | 771520006/1828329 | 771520006 | 1828329 | AMS | 2131931 | 02-NOV-16 06.47.43.000000000 AM AMERICA/PHOENIX | 21-JUL-21 12.00.00.000000000 AM AMERICA/PHOENIX | 28-JAN-22 02.33.49.000000000 PM AMERICA/PHOENIX | HISTORY | 24 | 1 | 05 | 1 | 5252B | 001 | | | | | | | 1784294 | | |
2131931 | 1828329 | 771520006/1828329 | 771520006 | 1828329 | AMS | 2131931 | 02-NOV-16 06.47.43.000000000 AM AMERICA/PHOENIX | 28-JAN-22 12.00.00.000000000 AM AMERICA/PHOENIX | 10-FEB-22 08.29.22.000000000 AM AMERICA/PHOENIX | HISTORY | 23 | 1 | 03 | 1 | 5252B | 001 | | | | | | | 1784295 | | |

and Connor said...

A quick option is to use DBMS_XSLPROCESSOR, eg

SQL> create table t ( x clob);

Table created.

SQL>
SQL> insert into t values (rpad('x',2000,'x'));

1 row created.

SQL>
SQL> declare
  2    c clob;
  3  begin
  4    select x into c from t;
  5    dbms_xslprocessor.clob2file(c,'TEMP','myclob.txt');
  6  end;
  7  /

PL/SQL procedure successfully completed.



Rating

  (1 rating)

Comments

Abhishek, September 04, 2023 - 6:26 am UTC

Thanks for the response, using the suggestion, I got the desired output
Connor McDonald
September 06, 2023 - 4:21 am UTC

glad we could help

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library