Si vous devez exécuter la même requête sur toutes les bases de données hébergées sur votre serveur, vous pouvez utiliser ce script bash.
Le script vérifie toutes les instances Oracle dans le fichier /etc/oratab, mais il ignore toutes les lignes commentées.
Il ignore également toutes les instances ASM.
Vous devez créer un script avec la requête que vous souhaitez exécuter et le passer en argument au script bash.
Créez un nouveau fichier nommé run_on_all.sh et mettez-y ce contenu :
#!/bin/bash
# Oratab location
ORATAB=/etc/oratab
# SQL script file passed as an argument
SQL_FILE=$1
# Log file with timestamp in filename
LOG_FILE="run_on_all_dbs_$(date +%Y%m%d_%H%M%S).log"
# Check if the oratab file exists
if [ ! -f "$ORATAB" ]; then
echo "Oratab file not found at $ORATAB" | tee -a $LOG_FILE
exit 1
fi
# Check if the SQL file exists
if [ ! -f "$SQL_FILE" ]; then
echo "SQL file not found at $SQL_FILE" | tee -a $LOG_FILE
exit 1
fi
# Iterate over databases in oratab
while read -r line
do
# Check for comment lines, instances starting with '+ASM', and blank lines
if [[ $line = \#* ]] || [[ $line = +ASM* ]] || [[ -z $line ]]; then
continue
fi
# Extract SID and HOME from oratab line
DB_SID=$(echo $line | cut -d':' -f1)
DB_HOME=$(echo $line | cut -d':' -f2)
echo "Running SQL script on database: $DB_SID" | tee -a $LOG_FILE
# Export necessary environment variables
export ORACLE_SID=$DB_SID
export ORACLE_HOME=$DB_HOME
export PATH=$ORACLE_HOME/bin:$PATH
# Run the SQL script and log the output
sqlplus -s / as sysdba @$SQL_FILE | tee -a $LOG_FILE
done < "$ORATAB"
Ensuite, vous lui donnez des droits d'exécution.
chmod +x run_on_all.sh
Et vous l'exécutez comme cela, en supposant que vous ayez un script Oracle appelé script.sql
./run_on_all.sh script.sql