如何格式化用sqlplus创建的输出文件?为什么其中一个输出块出现两次?

时间:2012-07-06 07:22:02

标签: xml oracle sqlplus

我使用此步骤Oracle 10g - Write queries results to file来创建xml文件。所以这是我加载的

的sql脚本

c:> sqlplus -s username /password@database.domain.com< tmp.sql> output.txt的

  set pagesize 0;
    set serveroutput on
    set termout off       
    set verify off     
    set heading off    
    set long 999
    set lines 999
    SET FEEDBACK OFF
    SET HEAD OFF

    SELECT '<?xml version="1.0" encoding="UTF-8"?>' || chr(10) ||
           '<!--Sample XML file generated by XMLSpy v2010 rel. 2 (http://www.altova.com)-->' || chr(10) ||
           '<HarpeML_CBS_IMX_ExchangeRate_Flow  xsi:noNamespaceSchemaLocation="HarpeML_CBS_IMX_ExchangeRate(REF-IMX-1)_v0.0.00.xsd"  xmlns:harpeml="http://www.harpeml.com"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'||chr(10)||
           '<Header>'||chr(10)||
           '<harpeml:technicalIndicator>'||'HDR'||'</harpeml:technicalIndicator>'||chr(10)||
           '<harpeml:orderNumber>'||''||lpad ( imk.nextval, 10, '0' )||'</harpeml:orderNumber>'||chr(10)||
           '<harpeml:dataSelectionDate>'||''||to_char(sysdate -  4,'DD/MM/YYYY')||''||'</harpeml:dataSelectionDate>'||chr(10)|| 
            '<harpeml:extractionTimeStamp>'||''||CURRENT_TIMESTAMP||''||'</harpeml:extractionTimeStamp>'||chr(50)||'</Header>'
            FROM dual;
    SELECT '<ExchangeRates>'||chr(10)||'<ExchangeRate>'||chr(10)||
           '<harpeml:technicalIndicator>'||'02'||'</harpeml:technicalIndicator>'||chr(10)||
           '<harpeml:currencyExchangeRateType>'||''||'D'||'</harpeml:currencyExchangeRateType>'||chr(10)
            FROM dual;
    SELECT  '<harpeml:baseCurrencyCode>'||abrev||'</harpeml:baseCurrencyCode>'  FROM(SELECT abrev FROM v_domaine where type = 'DEVISE' ORDER BY  dbms_random.normal)WHERE rownum = 1;
    SELECT 
            '<harpeml:counterCurrencyCode>'||abrev||'</harpeml:counterCurrencyCode>'FROM(SELECT  abrev FROM v_domaine where type = 'DEVISE' ORDER BY  dbms_random.normal)WHERE rownum = 1;
    SELECT 
            '<harpeml:startValidityDate>'||''||to_char(sysdate -  4,'DD/MM/YYYY')||''||'<harpeml:startValidityDate>'||chr(10)||
             '<harpeml:countryCode>'||abrev||'</harpeml:countryCode>'FROM(SELECT  abrev FROM v_domaine where type = 'pays' ORDER BY  dbms_random.normal)WHERE rownum = 1;   
    SELECT
            '<harpeml:exchangeRate>'||rpad ( imk.nextval, 3, '51' )||'</harpeml:exchangeRate>'||chr(10)||
            '<harpeml:appreciationOrDepreciationReport>'||'1'||'</harpeml:appreciationOrDepreciationReport>'||chr(10)||
            '<harpeml:dataSourceSystem>'||'freetext'||'</harpeml:dataSourceSystem>'||chr(10)||
            '</ExchangeRate>'||chr(10)||
            '</ExchangeRates>'||chr(10)||
            '</HarpeML_CBS_IMX_ExchangeRate_Flow>'
            from dual;
    /

结果如下:

<?xml version="1.0" encoding="UTF-8"?>
<!--Sample XML file generated by XMLSpy v2010 rel. 2 (http://www.altova.com)-->
<HarpeML_CBS_IMX_ExchangeRate_Flow  xsi:noNamespaceSchemaLocation="HarpeML_CBS_IMX_ExchangeRate(REF-IMX-1)_v0.0.00.xsd"  xmlns:harpeml="http://www.harpeml.com"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Header>
<harpeml:technicalIndicator>HDR</harpeml:technicalIndicator>
<harpeml:orderNumber>0000000161</harpeml:orderNumber>
<harpeml:dataSelectionDate>02/07/2012</harpeml:dataSelectionDate>
<harpeml:extractionTimeStamp>06-JUL-12 09.29.01.812631 AM +03:00</harpeml:extractionTimeStamp>2</Header>

<ExchangeRates>
<ExchangeRate>
<harpeml:technicalIndicator>02</harpeml:technicalIndicator>
<harpeml:currencyExchangeRateType>D</harpeml:currencyExchangeRateType>

<harpeml:baseCurrencyCode>BOB</harpeml:baseCurrencyCode>
<harpeml:counterCurrencyCode>SGD</harpeml:counterCurrencyCode>
<harpeml:startValidityDate>02/07/2012<harpeml:startValidityDate>
<harpeml:countryCode>BDI</harpeml:countryCode>

<harpeml:exchangeRate>162</harpeml:exchangeRate>
<harpeml:appreciationOrDepreciationReport>1</harpeml:appreciationOrDepreciationReport>
<harpeml:dataSourceSystem>freetext</harpeml:dataSourceSystem>
</ExchangeRate>
</ExchangeRates>
</HarpeML_CBS_IMX_ExchangeRate_Flow>

**<harpeml:exchangeRate>163</harpeml:exchangeRate>
<harpeml:appreciationOrDepreciationReport>1</harpeml:appreciationOrDepreciationReport>
<harpeml:dataSourceSystem>freetext</harpeml:dataSourceSystem>
</ExchangeRate>
</ExchangeRates>
</HarpeML_CBS_IMX_ExchangeRate_Flow>** 

有谁知道为什么最后一次出现两次?你能给出任何建议如何从输出文件中删除空行吗?

2 个答案:

答案 0 :(得分:3)

在SQL * Plus中,分​​号执行一个语句。斜线也是如此。

您已使用执行它们的;终止了每个选择。然后,您使用/终止了脚本,这会导致最后一个语句再次执行。

至于空行,这只是运行几个不同语句的工件。我不担心他们。当然XML并不关心。

Hawever这是一种生成XML文件的繁琐方式:Oracle拥有大量XML功能:you should learn to use them

答案 1 :(得分:2)

最后一个块出现两次,因为脚本末尾有一个/ - 这会再次执行上一个查询。

只需将/替换为退出,就应该解决这个问题。