0% found this document useful (0 votes)
38 views2 pages

SQL and Python Deployment Guide

The document outlines a two-step deployment process involving a SQL script that creates or updates a table and a Python script that executes this SQL script on multiple SQL servers. It includes instructions for setting up the environment, such as installing necessary packages and configuring server details. The Python script reads the SQL file and deploys it sequentially to each specified server, handling both successful deployments and errors.

Uploaded by

alpsughasiny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views2 pages

SQL and Python Deployment Guide

The document outlines a two-step deployment process involving a SQL script that creates or updates a table and a Python script that executes this SQL script on multiple SQL servers. It includes instructions for setting up the environment, such as installing necessary packages and configuring server details. The Python script reads the SQL file and deploys it sequentially to each specified server, handling both successful deployments and errors.

Uploaded by

alpsughasiny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Step 1: The SQL Deployment Script

=================================

-- Create or update a table


IF NOT EXISTS (
SELECT * FROM [Link] WHERE name = 'TestDeploy'
)
BEGIN
CREATE TABLE TestDeploy (
ID INT PRIMARY KEY,
Message NVARCHAR(200)
);
END
-- Insert or update sample data
MERGE INTO TestDeploy AS target
USING (SELECT 1 AS ID, 'Deployed successfully!' AS Message) AS source
ON [Link] = [Link]
WHEN MATCHED THEN
UPDATE SET [Link] = [Link]
WHEN NOT MATCHED THEN
INSERT (ID, Message) VALUES ([Link], [Link]);

Step 2: The Python Deployment Script


====================================

import pyodbc
import time

# Configuration
sql_script_path = "deployment_script.sql" # Path to your .sql file

sql_servers = [
{
"name": "DSIT_1",
"server": "localhost",
"database": "DSIT_DB1",
"username": "userid",
"password": "password"
},
{
"name": "DSIT_2",
"server": "[Link]",
"database": "DSIT_DB2",
"username": "user",
"password": "pass"
},
# Add more databases as needed
]

def run_script_on_server(config, script):


print(f"\nDeploying to {config['name']}...")

conn_str = (
f"DRIVER={{ODBC Driver 17 for SQL Server}};"
f"SERVER={config['server']};"
f"DATABASE={config['database']};"
f"UID={config['username']};"
f"PWD={config['password']};"
)

try:
with [Link](conn_str, autocommit=False) as conn:
cursor = [Link]()
[Link](script) # Run full script as a batch
[Link]()
print(f"Deployment successful for {config['name']}")
except Exception as e:
print(f"Deployment failed for {config['name']}: {e}")

def load_sql_script(path):
with open(path, "r", encoding="utf-8") as f:
return [Link]()

if __name__ == "__main__":
print("Starting deployment using per-server SQL credentials...")

sql_script = load_sql_script(sql_script_path)

for config in sql_servers:


run_script_on_server(config, sql_script)
[Link](1)

print("\nAll deployments complete.")

Deployment Steps
----------------
1. Install the required Python package:
pip install pyodbc
2. Ensure the ODBC Driver 17 for SQL Server is installed on your system.
3. Place both [Link] and deployment_script.sql in the same directory.
4. Update the sql_servers list with the connection details for each target
database.
5. Run the script:
python [Link]
6. The script will read the SQL file once and deploy it to each listed server in
sequence.

You might also like