【发布时间】:2015-02-28 17:33:39
【问题描述】:
我想使用 shell 脚本自动化包含多个 SQL 查询的 SQL 文件,同时我想从中获取 last Query 的 Output将 SQL 文件转换为任何平面文件。
【问题讨论】:
我想使用 shell 脚本自动化包含多个 SQL 查询的 SQL 文件,同时我想从中获取 last Query 的 Output将 SQL 文件转换为任何平面文件。
【问题讨论】:
您可以创建一个文件来执行每个查询,但只发送最后一个查询的输出。
#!/bin/bash
mysql -h... -u... -p... -e 'query1' > /dev/null
mysql -h... -u... -p... -e 'query2' > /dev/null
mysql -h... -u... -p... -e 'query3' > /result.sql
【讨论】:
此awk 命令将在文件中查找最后一个以分号分隔的查询并将其通过管道传送到mysql
awk 'BEGIN {RS=";"} NF > 0 {query=$0; } END {print query}' file.sql | mysql -u username -p password > output.txt
NF > 0 防止将query 设置为最后一个; 之后的空行。
【讨论】:
select count (*) from custcomps group by 1 having count(*) > 50 ; select count ( unique(Siebel_Ac_No)) from custcomps ; show select * from custcomps ; select * from "EDWPRDE_VW40_OLAP_CMPLNT"."SR_STTS_TYPE_DIM" show select PIH."Party_Idntn_Num" from EDWPRDE_VW40_OLAP_CMPLNT.CMPLNT_DTL CMPS inner join EDWPRDE_VW40_PUB.T01005_Party_IDNTN_HIST PIH on PIH.Party_Id = CMPS."Customer Id" and PIH.Party_Idntn_Type_Id = 2 and PIH.Party_Idntn_End_Dttm = '9999-12-31 23:59:59' where CMPS."As-At Date Id" = current_date and CMPS."Created Date Id" > current_date-365 group by 1;
awk 来查找基于分号的查询。