Loop through result set of mysql in Bash - routines

xesrikrc  于 2022-12-22  发布在  Mysql
关注(0)|答案(2)|浏览(132)

I want to make specific .sql file for each of my routines.

#!/bin/bash

 routine_names=$(mysql mydb  --execute="SELECT 
    *
FROM
    information_schema.routines
WHERE
    routine_type = 'PROCEDURE' OR routine_type ='FUNCTION'
        AND routine_schema = 'mydb';"|cut -f1)

for routine in "$routine_names"
do
    if [ -e "${routine}.sql" ]
     then
    echo "ok"
    else
     content_procedure=$(mysql mydb--execute="SHOW CREATE PROCEDURE $routine;")
     echo "$content_procedure" >> masoud.txt
   fi   
done

my routine_names variable is a list of my procedures. like this:

SP_ONE
SP_TWO
SP_THREE

I want to loop of these result. but I think the result is not an array. because routine variable has all content.

wko9yo5t

wko9yo5t1#

wrap your mysql mydb ... command with ()

routine_names=($(mysql mydb  --execute="SELECT 
    *
FROM
    information_schema.routines
WHERE
    routine_type = 'PROCEDURE' OR routine_type ='FUNCTION'
        AND routine_schema = 'mydb';"|cut -f1))

I was curious and got the idea from here .

xqkwcwgp

xqkwcwgp2#

Just another case how I fix my problem

#!/bin/bash
mysql -u USER -p -h localhost -D database1 -e "SELECT ID FROM prodTable WHERE display=1 AND new=1 AND exDate<DATE_SUB(CURDATE(),INTERVAL 2 YEAR)" | while read ID;
do 
      echo "The following product has been moved: $ID"
done

相关问题