forked from silasmendes/python-library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql-execute-list-of-commands-serially.py
More file actions
32 lines (29 loc) · 1.29 KB
/
sql-execute-list-of-commands-serially.py
File metadata and controls
32 lines (29 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Scenario
# I have a CSV file that contains hundreds of SQL commands that I want to run serially.
# Each command will be executed in the context of a new connection in Azure SQL Database.
# This script requires ODBC Driver 17 for SQL Server
import csv
import pyodbc
server = 'server_name.database.windows.net'
database = 'database_name'
username = 'SQL auth username'
password = 'password'
driver= '{ODBC Driver 17 for SQL Server}'
# Replace 'your_csv_file.csv' with the path to your actual CSV file
with open('your_csv_file.csv', newline='') as csvfile:
commands = csv.reader(csvfile, delimiter='\n', quotechar='"')
for row in commands:
command = row[0]
if command.strip() != '':
try:
conn = pyodbc.connect('DRIVER=' + driver + ';SERVER=' + server + ';DATABASE=' + database + ';UID=' + username + ';PWD=' + password)
cursor = conn.cursor()
cursor.execute(command)
print('Executed command:', command)
print('Execution successful')
except Exception as e:
print('Error executing command:', command)
print('Error message:', str(e))
finally:
cursor.close()
conn.close()