linux - 用于在 csv 文件中获取 sql 查询数据的 Shell 脚本

标签 linux bash shell scripting

需要使用 shell 脚本提取以下查询数据以及 csv 文件中的 header 。

下面是查询。

SELECT SourceIdentifier,SourceFileName,ProfitCentre2,PlantCode,
tax_retur ReturnPeriod,document_number DocumentNumber,TO_CHAR(invoice_generation_date,'YYYY-MM-DD')
Docume,Original,customer_name CustomerName,NVL(sns_pos,new_state_code)POS,PortCode,NEW_HSN_CODE HSNorSAC,(SGSATE+UTGSATE) Stat,(SGS+UT)StateUT,Userde FROM arbor.INV_REPO_FINA WHERE UPPER(document_type)='INV' AND UPPER(backout_flag)='VALID' AND new_gst_id_new IS NOT NULL AND new_charges<>0 AND taxable_adj=0
UNION
SELECT SourceIdentifier,SourceFileName,ProfitCentre2,PlantCode,
tax_retur ReturnPeriod,document_number DocumentNumber,TO_CHAR(invoice_generation_date,'YYYY-MM-DD')
Docume,Original,customer_name CustomerName,NVL(sns_pos,new_state_code)POS,PortCode, NEW_HSN_CODE HSNorSAC,(SGSATE+UTGSATE) Stat,(SGS+UTG)StateUT,Userde FROM arbor.INV_REPO_FINA WHERE UPPER(document_type)='INV' AND UPPER(backout_flag)='VALID' AND new_gst_id_new IS NOT NULL AND new_charges<>0 AND  taxable_adj<>0

请告诉我以下使用 shell 脚本获取数据的方法是否正确并且脚本是否正确。

#!/bin/bash
file="output.csv"
sqlplus -s username/password@Oracle_SID << EOF
SPOOL $file

select 'SourceIdentifier','SourceFileName','ProfitCentre2','PlantCode',
'tax_retur ReturnPeriod','document_number DocumentNumber','TO_CHAR(invoice_generation_date,'YYYY-MM-DD') Docume','Original','customer_name CustomerName','NVL(sns_pos,new_state_code)POS','PortCode','NEW_HSN_CODE HSNorSAC','(SGSATE+UTGSATE) Stat','(SGS+UT)StateUT','Userde' from dual
Union all
select 'TO_CHAR(SourceIdentifier)','TO_CHAR(SourceFileName)','TO_CHAR(ProfitCentre2)','TO_CHAR(PlantCode)',
'TO_CHAR(tax_retur ReturnPeriod)','TO_CHAR(document_number DocumentNumber)','TO_CHAR(invoice_generation_date,'YYYY-MM-DD')
Docume','TO_CHAR(Original)','TO_CHAR(customer_name CustomerName)','TO_CHAR(NVL(sns_pos,new_state_code)POS)','TO_CHAR(PortCode)','TO_CHAR(NEW_HSN_CODE HSNorSAC)','TO_CHAR((SGSATE+UTGSATE) Stat)','TO_CHAR((SGS+UT)StateUT)','TO_CHAR(Userde)' from
(SELECT SourceIdentifier,SourceFileName,ProfitCentre2,PlantCode,
tax_retur ReturnPeriod,document_number DocumentNumber,TO_CHAR(invoice_generation_date,'YYYY-MM-DD')
Docume,Original,customer_name CustomerName,NVL(sns_pos,new_state_code)POS,PortCode,NEW_HSN_CODE HSNorSAC,(SGSATE+UTGSATE) Stat,(SGS+UT)StateUT,Userde FROM arbor.INV_REPO_FINA WHERE UPPER(document_type)='INV' AND UPPER(backout_flag)='VALID' AND new_gst_id_new IS NOT NULL AND new_charges<>0 AND taxable_adj=0
UNION
SELECT SourceIdentifier,SourceFileName,ProfitCentre2,PlantCode,
tax_retur ReturnPeriod,document_number DocumentNumber,TO_CHAR(invoice_generation_date,'YYYY-MM-DD')
Docume,Original,customer_name CustomerName,NVL(sns_pos,new_state_code)POS,PortCode, NEW_HSN_CODE HSNorSAC,(SGSATE+UTGSATE) Stat,(SGS+UTG)StateUT,Userde FROM arbor.INV_REPO_FINA WHERE UPPER(document_type)='INV' AND UPPER(backout_flag)='VALID' AND new_gst_id_new IS NOT NULL AND new_charges<>0 AND  taxable_adj<>0)

SPOOL OFF
EXIT
EOF

最佳答案

简而言之:; select 语句末尾缺少。

一些未请求的建议:

我认为 spool 会将额外的内容放入您的文件中(至少是一些新行),重定向更好,而且第一行与数据库无关:

echo "SourceIdentifier;SourceFileName;ProfitCentre2..." > $file

我建议直接在选择查询中生成 csv 格式,稍后会更令人头疼(你可以在那里转义你想要的):

$query = "select SourceIdentifier || ';' || SourceFileName || ';' || ProfitCentre2 ... ;"

所以查询数据库(我认为大写 -S 是正确的)加上记录的格式(也许你也想格式化你的列):

sqlplus -S username/password@Oracle_SID >> $file << EOF
set linesize 32767 pagesize 0 heading off
$query
EOF

关于linux - 用于在 csv 文件中获取 sql 查询数据的 Shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49790661/

相关文章:

linux - 使用标准 shell 构建自定义应用程序特定的 shell

java - 如何在以 root 身份执行的可执行 jar 内以用户模式运行脚本?

shell - 如何从 LaTeX 执行 shell 脚本?

linux - exec n<&m 与 exec n>&m —— 基于 Sobell 的 Linux 书

Bash:限制并发作业的数量?

linux - 无效的 mex 文件 : undefined symbol: cholmod_camd

Linux Bash sed 命令

linux - 通过 Azure 自动化在 Azure Linux VM 上运行 SSH 命令

linux - 在 ubuntu 中编辑 bashrc 文件后命令不起作用

linux - 如何在 linux 中传入读取的 bash 变量并将其作为消息发送到 git commit 中?