This Python script connects to an SQL Server database using pyodbc
, executes multiple queries, and exports the results to CSV files. It provides an easy way to extract data from SQL tables and store them as structured CSV files for further analysis.
Ensure you have the following installed before running the script:
- Python 3.x
- Required Python libraries:
pyodbc
pandas
You can install the necessary dependencies using:
pip install pyodbc pandas
- Modify the connection details in RunListofSQLQueries.py:
- Update server with your SQL Server hostname.
- Update database with your database name.
- Ensure your driver ({SQL Server}) is correct.
- Define the SQL queries.
queries = [
{"query": "SELECT * FROM [TABLE1]", "filename": "Table1export.csv"},
{"query": "SELECT * FROM [TABLE2]", "filename": "Table2export.csv"},
{"query": "SELECT * FROM [TABLE3]", "filename": "Table3export.csv"}
]
- Run the script (RunListofSQLQueries.py)
- Check the generated CSV files in the same directory.
Running query: SELECT * FROM [TABLE1]
Saved results to Table1export.csv
Running query: SELECT * FROM [TABLE2]
Saved results to Table2export.csv
Running query: SELECT * FROM [TABLE3]
Saved results to Table3export.csv
All queries executed successfully.
If you encounter a driver error, check your installed SQL Server ODBC drivers using:
odbcinst -q -d
This project is licensed under the MIT License.