0

Currently, I am reading few queries from the below .sql file

--SOURCE TABLE NAME  
--SOURCE QUERY  
SEL COL1, COL2, COL3, COL4,   
COL5, COL6, COL7 WHERE COL5 = '2015-11-04 16:24:00.000000' FROM SOURCE TABLE;

--TARGET TABLE NAME  
--TARGET QUERY  
SEL COLUMN1, COLUMN2, COLUMN3, COLUMN4,   
COLUMN5, COLUMN6, COLUMN7 FROM TARGET TABLE;  
0,1

The code used to read the contents of the .sql file is being displayed below:

validate() {  
queryNum=0  
while true  
do  
    ((queryNum++))  
    read tableName  
    read comment  
    read sourceQuery   
    read blankLine  
    read tableName  
    read comment  
    read targetQuery   
    read primaryKeyCols || break  
    read blankLine  
    exQuery "$sourceQuery" sourceResults.txt   
    exQuery "$targetQuery" targetResults.txt           
done < $1  
}  

The only disadvantage with this approach is that I can't read the SQL query if it is written in multi lines. It has to be on a single line to make it work.

I want to be more flexible while reading the queries. Is there any way that I can read the multi line SQL in Unix. Please help me out.

3
  • 2
    Is there a reason you can't just send the SQL to your SQL client (e. g. mysql < /path/to/myfile.sql)?
    – DopeGhoti
    Nov 30, 2015 at 18:53
  • Please explain, what you want to do. Nov 30, 2015 at 19:33
  • I just want to have that flexibility in reading those queries in multiple lines, its causing a havoc if I miss a single space in any of those lines ..I am using Teradata btw.. Nov 30, 2015 at 20:08

1 Answer 1

0

A simple approach, assuming the ; is the last character of the line, would be something like

sqlcommand=""
while read -r line; do
        sqlline=${line%%--*}
        sqlcommand+="${sqlline}"
        if [[ "${sqlline}" = *\;* ]]; then
                break
        fi
done < input

echo "${sqlcommand}"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .