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.

1 Answers1

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}"
Walter A
  • 736