Find OBJECTS IN FOLDER

import os

import re


# Path to the directory containing SQL scripts

directory_path = '/path/to/sql/scripts'


# Regex pattern to match table names (very basic for demonstration)

# This pattern looks for words that come after "FROM" or "JOIN" followed by optional schema names

pattern = re.compile(r'\bFROM\b\s+([a-zA-Z0-9_\.]+)|\bJOIN\b\s+([a-zA-Z0-9_\.]+)', re.IGNORECASE)


# Set to store unique object names

object_names = set()


# Loop through each file in the directory

for filename in os.listdir(directory_path):

    if filename.endswith('.sql'):  # Check if the file is a SQL file

        filepath = os.path.join(directory_path, filename)

        with open(filepath, 'r') as file:

            content = file.read()

            # Find all matches in the file content

            matches = pattern.findall(content)

            for match in matches:

                # Each match is a tuple because of the groups in the pattern; we add both

                object_names.update(match)


# Remove empty strings that may have been added to the set

object_names.discard('')


print("Found object names:")

for name in object_names:

    print(name)


Run .SQL SCRIPT 

import cx_Oracle


# Database connection parameters

dsn = 'YourDSN'  # Data Source Name or Easy Connect string

username = 'your_username'

password = 'your_password'


# Path to your SQL file (assumed to be in the same folder as this script)

sql_file_path = 'your_script.sql'


# Connect to the database

connection = cx_Oracle.connect(username, password, dsn)

cursor = connection.cursor()


# Open and read the SQL file

with open(sql_file_path, 'r') as sql_file:

    sql_script = sql_file.read()


# Execute the SQL script

# Note: This simplistic approach assumes the SQL file contains a single SQL statement or PL/SQL block.

# For multiple statements, you'll need to split the script and execute them individually.

try:

    cursor.execute(sql_script)

    connection.commit()  # Commit changes if any DML statements are executed

except cx_Oracle.DatabaseError as e:

    print(f"An error occurred: {e}")

finally:

    cursor.close()

    connection.close()


print("SQL script executed successfully.")