简体   繁体   中英

SQL update from looping through a text file

At the moment I am doing:

cat routes.csv | while read a b; do

echo "... changing route on account $a to $b"

sqlplus -s $user/$pass@$db <<EOF
UPDATE customers SET delivery_route = '$b' WHERE customer_no = '$a';
commit;
quit;
EOF

echo "... done"
echo " "

done

This creates a new SQL connection for every line that it processes and I have 500+ accounts that need to be updated.

Would it be a quicker process to open a new SQL connection, run all the updates and close? If so how would I do that?

cat routes.csv | while read a b; do
echo "UPDATE customers SET delivery_route = '$b' WHERE customer_no = '$a';"
done | sqlplus -s $user/$pass@$db

will use a single mysql client process, a single connection and a single transaction, so it will be way faster than forking a different process for every entry

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM