randomuser1
randomuser1

Reputation: 2803

how can I run multiple sql statements one after another in bash?

In my bash script I have a list of SQL queries defined as follows:

#!/bin/bash

declare -a queriesToRun=("SELECT count(*) from table1" "SELECT count(*) from table2" "select count(*) from table3")

I would like to run each statement (one after another) and see the output on the console. I tried:

db2 connect to mydb

for val in ${queriesToRun[@]}; do

cat > "temp_script" << END
--#SET TERMINATOR @
$val@
END

echo `db2 -ntd~ -f temp_script`

rm temp_script

done

but that didn't do the trick. Is there any other/easier way to do so? Thanks!

Upvotes: 0

Views: 499

Answers (1)

Gilles Qu&#233;not
Gilles Qu&#233;not

Reputation: 185161

Try this :

db2 connect to mydb

declare -a queriesToRun=(
    'SELECT count(*) from table1'
    'SELECT count(*) from table2'
    'select count(*) from table3'
)

for val in "${queriesToRun[@]}"; do
    db2 -ntd~ -f /dev/stdin <<EOF
    --#SET TERMINATOR @
    ${val}@
EOF
done

Upvotes: 2

Related Questions