╔═════════╦═════════════════════════════════════════╗
║ Command ║ Description ║
╠═════════╬═════════════════════════════════════════╣
║ -S ║ [protocol:]server[instance_name][,port] ║
║ -U ║ login_id ║
║ -p ║ password ║
║ -Q ║ "cmdline query" (and exit) ║
╚═════════╩═════════════════════════════════════════╝
╔═════════╦═════════════════════════════════════════╗
║ Command ║ Description ║
╠═════════╬═════════════════════════════════════════╣
║ -S ║ [protocol:]server[instance_name][,port] ║
║ -U ║ login_id ║
║ -p ║ password ║
║ -Q ║ "cmdline query" (and exit) ║
╚═════════╩═════════════════════════════════════════╝
Try with pymssql: pip install pymssql
import pymssql
try:
conn = pymssql.connect(server="host_or_ip", user="your_username", password="your_username"password="your_password", database="your_db")
cursor = conn.cursor()
cursor.execute ("SELECT @@VERSION")
row = cursor.fetchone()
print(f"\n\nSERVER VERSION:\n\n{row[0]}")
cursor.close()
conn.close()
except Exception:
print("\nERROR: Unable to connect to the server.")
exit(-1)
Output:
SERVER VERSION:
Microsoft SQL Server 2016 (SP2-CU14) (KB4564903) - 13.0.5830.85 (X64)
Jul 31 2020 18:47:07
Copyright (c) Microsoft Corporation
Standard Edition (64-bit) on Windows Server 2012 R2 Standard 6.3 <X64> (Build 9600: ) (Hypervisor)
The connection can also be checked from the terminal, with a single line of code with sqlcmd. See syntax .
sqlcmd -S "host_or_ip" -U "your_username" -p -Q "SELECT @@VERSION"
output:
Password: your_password
--------------------------------------------------------------------------------------------------------------------------------------------------------
Microsoft SQL Server 2016 (SP2-CU14) (KB4564903) - 13.0.5830.85 (X64)
Jul 31 2020 18:47:07
Copyright (c) Microsoft Corporation
Standard Edition (64-bit) on Windows Server 2012 R2 Standard 6.3 <X64> (Build 9600: ) (Hypervisor)
(1 rows affected)
Network packet size (bytes): 4096
1 xact[s]:
Clock Time (ms.): total 1 avg 1.00 (1000.00 xacts per sec.)
Try with pymssql: pip install pymssql
import pymssql
try:
conn = pymssql.connect(server="host_or_ip", user="your_username", password="your_username", database="your_db")
cursor = conn.cursor()
cursor.execute ("SELECT @@VERSION")
row = cursor.fetchone()
print(f"\n\nSERVER VERSION:\n\n{row[0]}")
cursor.close()
conn.close()
except Exception:
print("\nERROR: Unable to connect to the server.")
exit(-1)
Output:
SERVER VERSION:
Microsoft SQL Server 2016 (SP2-CU14) (KB4564903) - 13.0.5830.85 (X64)
Jul 31 2020 18:47:07
Copyright (c) Microsoft Corporation
Standard Edition (64-bit) on Windows Server 2012 R2 Standard 6.3 <X64> (Build 9600: ) (Hypervisor)
Try with pymssql: pip install pymssql
import pymssql
try:
conn = pymssql.connect(server="host_or_ip", user="your_username", password="your_password", database="your_db")
cursor = conn.cursor()
cursor.execute ("SELECT @@VERSION")
row = cursor.fetchone()
print(f"\n\nSERVER VERSION:\n\n{row[0]}")
cursor.close()
conn.close()
except Exception:
print("\nERROR: Unable to connect to the server.")
exit(-1)
Output:
SERVER VERSION:
Microsoft SQL Server 2016 (SP2-CU14) (KB4564903) - 13.0.5830.85 (X64)
Jul 31 2020 18:47:07
Copyright (c) Microsoft Corporation
Standard Edition (64-bit) on Windows Server 2012 R2 Standard 6.3 <X64> (Build 9600: ) (Hypervisor)
The connection can also be checked from the terminal, with a single line of code with sqlcmd. See syntax .
sqlcmd -S "host_or_ip" -U "your_username" -p -Q "SELECT @@VERSION"
output:
Password: your_password
--------------------------------------------------------------------------------------------------------------------------------------------------------
Microsoft SQL Server 2016 (SP2-CU14) (KB4564903) - 13.0.5830.85 (X64)
Jul 31 2020 18:47:07
Copyright (c) Microsoft Corporation
Standard Edition (64-bit) on Windows Server 2012 R2 Standard 6.3 <X64> (Build 9600: ) (Hypervisor)
(1 rows affected)
Network packet size (bytes): 4096
1 xact[s]:
Clock Time (ms.): total 1 avg 1.00 (1000.00 xacts per sec.)
Try with pymssql: pip install pymssql
import pymssql
try:
conn = pymssql.connect(server="host_or_ip", user="your_username", password="your_username", database="your_db")
cursor = conn.cursor()
cursor.execute ("SELECT @@VERSION")
row = cursor.fetchone()
print(f"\n\nSERVER VERSION:\n\n{row[0]}")
cursor.close()
conn.close()
except Exception:
print("\nERROR: Unable to connect to the server.")
exit(-1)
Output:
SERVER VERSION:
Microsoft SQL Server 2016 (SP2-CU14) (KB4564903) - 13.0.5830.85 (X64)
Jul 31 2020 18:47:07
Copyright (c) Microsoft Corporation
Standard Edition (64-bit) on Windows Server 2012 R2 Standard 6.3 <X64> (Build 9600: ) (Hypervisor)
default