Si necesita ejecutar la misma consulta en todas las bases de datos alojadas en su servidor, puede utilizar este script bash.
El script comprueba todas las instancias de Oracle en /etc/oratab, pero se salta cualquier línea comentada.
También omite todas las instancias ASM.
Debe crear un script con la consulta que desea ejecutar y pasarlo como argumento al script bash.
Crea un nuevo archivo llamado run_on_all.sh y pon este contenido dentro:
#!/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"
Entonces le das derechos de ejecución.
chmod +x run_on_all.sh
Y lo ejecutas así, suponiendo que tienes un script de Oracle llamado script.sql
./run_on_all.sh script.sql