Passing value of variable to a query within shell script

I have a script in which i connect to database to run a query and get the result of the query to a temp file. This works fine , now what i want is there is flat file which contains the value to be used in the query. I want to read this file line by line and then run the query for each value in that file.

Example

Lets say Flat file(abc.txt) contains employee number
713
845
81

I need to run this query within a script

select * from emptab where emp_no="value from the file"

The present script runs fine if i pass value like this

eusql<< EOF
select * from emptab where emp_no in ('713','845','81')
go > tmp.txt
EOF

But i cant edit the code always so i would like to have this edited within a flat file and use the same in the script.

I tried with this but it didn't work

While read val
do
eusql<< EOF
select * from emptab where emp_no="$val"
go > tmp.txt
EOF
done < abc.txt

Thanks in advance.

What is $val ? Bits of it may need escaping.

Why is while capitalized?

Was your code intended at all when you tried it?

for emp_no in $(<file)
do
  echo "select * from emptab where emp_no=${emp_no}"
done > query.sql

Emanuel

You don't need a for-loop for that, better to use a while.

while read emp_no
do
        echo "select * from emptab where emp_no=${emp_no}"
done < inputfile > query.sql

Is there a good reason for that?

Emanuele

Yes. 'for' splits across whitespace, read splits across newlines, a subtle but important difference.

Also, 'while' is more efficient because it reads a line at a time instead of cramming the entire arbitrary sized file into memory at once.

Ok, thank you.

Emanuele

Thanks... It worked