标头在csv文件中生成问题

时间:2018-08-28 08:20:08

标签: sql oracle shell

我有一个只有列的.SQL文件。我需要生成一个csv文件,并且所有列都应该在一行中,但是问题是标题部分分成了许多行。我现在应该做什么来修复问题出在哪里?此sql文件将由shell脚本生成。

这是我的SQL文件。

    set heading off
set pagesize 0
set linesize 1000
set trimspool on
set feedback off


set colsep ","

column filename new_val filename
SELECT 'ReporteContratosFija_CBiO_'||TO_CHAR(SYSDATE,'YYYYMMDDHH24MISS')||'.csv' filename FROM dual;

--spool $REPORT_HOME/generated_reports/&filename
spool $REPORT_GEN_PATH/&filename

-- This SQL code retrieves from NK Table to generate MRC details after the bill run
select 'Customer id'||','||' Carry over period length'||','||' 
Carry over period unit of measure'||','||' Small invoice omission amount value'||','||'
Small invoice omission amount currency'||','||' Family'||','||' Customer status'||','||' 
Customer status reason'||','||' Customer status date'||','||' Customer level'||','||' 
Parent customer'||','||' Is payment responsible?, Payment responsible'||','||' 
Is contract responsible ?, Price group'||','||' OCC Rate plan'||','||' Customer Area'||','||' 
Trade code'||','||' Cost center'||','||' Customer payable currency'||','||' Conversion rate type'||','||' 
Customer tax category'||','||' Customer jurisdiction'||','||' Billing Cycle'||','||' Las bill cycle run for the customer'||','||' 
Current balance'||','||'Previous balance'||','||' Unbilled amount'||','||' Collection indicator'||','||' Customer First name'||','||' 
Customer Middle name'||','||' Customer Last name'||','||' Company name'||','||'Street'||','||' Street number'||','||' Postal code'||','||' City'||','||' 
State'||','||' County'||','||' Country'||','||' Is employee ?, Job description'||','||' Phone number 1'||','||' Phone number 2'||','||' Fax'||','||' Email'||','||' 
Mobile for SMS'||','||'Customer document'||','||' Customer document type'||','||' Customer nationality'||','||' Customer type'||','||' 
Billing account code'||','||' Billing account name'||','||' Bill Medium'||','||' Billing account contact first name'||','||' 
Billing account contact midle name'||','||' Billing account contact last name'||','||' 
Billing account company name'||','||' Billing account postal street'||','||' 
Billing account postal street number'||','||' Billing account postal code'||','||' 
Billing account postal city'||','||' Billing account postal state'||','||' 
Billing account postal county'||','||' Billing account postal country'||','||' 
Billing account contact job description'||','||' 
Billing account contact phone number 1'||','||'
 Billing account contact phone number 2'||','||'
 Billing account contact fax'||','||' Billing account contact email'||','||' 
 Billing account contact mobile for SMS'||','||' Billing account contact document'||','||' 
 Billing account contact document type'||','||' Billing account contact nationality'||','||'
 BSCS Contract Id'||','||' Tinko Contract Id'||','||' Contract user'||','||' 
 Contract Charging system id'||','||' SDP ID'||','||' Contract signed date'||','||' 
 Contract installation date'||','||' Contract Creation Date'||','||' 
 Contract current status'||','||' Contract current status reason'||','||'
 Contract current status valid from date'||','||' 
 Contract user first name'||','||'
 Contract user middle name'||','||' Contract user last name'||','||' 
 Contract user company name'||','||' Contract installation postal street'||','||'
 Contract installation postal street number'||','||' Contract installation postal code'||','||' 
 Contract installation postal city'||','||' Contract installation postal state'||','||'
 Contract installation postal county'||','||' Contract installation postal country'||','||' 
 Contract user job description'||','||' Contract user contact phone number 1'||','||'
 Contract user contact phone number 2'||','||' Contract user contact fax'||','||' 
 Contract user contact email'||','||' Contract user contact mobile for SMS'||','||' 
 Contract user document'||','||' Contract user document type'||','||'
 Contract user nationality'||','||' PO Name'||','||' Product instance id'||','||' 
 Catalog offer id'||','||' CFSS ID'||','||' CFSS Name'||','||' RFSS ID'||','||' RFSS Name'||','||' 
 Offer type'||','||' PO Element'||','||' PO Element name'||','||' One time charge amount'||','||' 
 Onte time charge amount currency'||','||' Recurrent charge amount'||','||' 
 Recurrent charge amount currency'||','||' Personalized one time charge amount'||','||' 
 Personalized one time charge indicator'||','||' Personalized one time charge amount currency'||','||'
 Personalized recurrent charge amount '||','||' Personalized charge indicator'||','||' 
 Personalized recurrent charge amount currency'||','||' Personalized recurrent charge period ' from dual;

spool off

--INSERT INTO NK_SHELL_EXECUTION_HISTORY(SCRIPT_NAME'||','||'LAST_EXECUTION_TIMESTAMP) VALUES ('NK_EXTRACT_CONTRACT_DATA.sh',TO_CHAR(SYSDATE,'YYYYMMDDHH24MISS'));
--COMMIT;

/

Shell脚本文件

#!/bin/sh
#export SCRIPT_PATH="/scripts/exeMRCReportFile.sh"
#sh ./scripts/exeMRCReportFile.sh

#echo "procedure execution completed"

echo Script Name: "$0"
echo Total Number of Argument Passed: "$#"
echo Arguments List -
echo 1. $1
if test -d $1;then

        echo "Welcome $1"


else

        echo "Not Exist";


        fi


echo All Arguments are: "$*"


ORACLE_SID=RTXNP2
export ORACLE_SID

export PATH=.:$PATH:$ORACLE_HOME/bin

export REPORT_HOME=$BSCS_WORKDIR/Reports/ANT001A_REQ01


if [ $# -ne 0 ];then

export REPORT_GEN_PATH=$1
echo "$REPORT_GEN_PATH"

else 


export REPORT_GEN_PATH=$WORK/Reports/ANT001A_REQ01/generated_reports
echo "$REPORT_GEN_PATH"
fi

##read -p 'Enter the database password for SYSADM user : ' password



echo "Please wait....The FTTH report is being created."
sqlplus SYSADM/SYSADM@$ORACLE_SID << eof_disp > $REPORT_HOME/logs/log_file_$$.log
        @$REPORT_HOME/scripts/GEN_FTTH_REPORT_FILE.sql
eof_disp

echo "FTTH report created."

##echo "Sftp for for MRC report started"
##./util/sftpMRCReportFile.sh
##echo "Sftp for MRC report done"

1 个答案:

答案 0 :(得分:1)

您的查询在所选字符串中包含换行符,您可以在其中执行以下操作:

select 'Customer id'||','||' Carry over period length'||','||' 
Carry over period unit of measure'||','||' Small invoice omission amount value'||','||'
Small invoice omission amount currency'||','||' Family'||','||' Customer status'||','||' 
...

在每个查询行的末尾,您打开下一个要连接的字符串,因此查询行以||'结尾,然后在查询的内部 换行-构成您生成的字符串值的一部分。

因此,您有效地正在做:

select 'Customer id'||','||' Carry over period length'||','||chr(10)||'Carry over period...

您可以仅更改查询中的开头引号和/或串联运算符的位置,以便换行符不属于字符串:

select 'Customer id'||','||' Carry over period length'||','|| 
'Carry over period unit of measure'||','||' Small invoice omission amount value'||','||
'Small invoice omission amount currency'||','||' Family'||','||' Customer status'||','||
'...

select 'Customer id'||','||' Carry over period length'||','
||'Carry over period unit of measure'||','||' Small invoice omission amount value'||','
||'Small invoice omission amount currency'||','||' Family'||','||' Customer status'||','
||'...

或者您也可以将最终的逗号移动到下一个查询行,这取决于您希望如何组织它们。

您不需要为每个值使用单独的字符串和串联,但是我想您知道这一点,并且出于风格原因对其进行了命名,而不是说:

select 'Customer id,Carry over period length' 
||',Carry over period unit of measure,Small invoice omission amount value'
||',Small invoice omission amount currency,Family,Customer status'
||',...