Ok, hopefully this is something really silly that I'm overlooking...
I'm using Python 3.7.2 and Pyodbc 4.0.24 on SQL Server 17 (14.0.2002.14)
On my SQL Server I create a table as such:
create table test(
test1 varchar(5)
)
But when I try to throw an error (because the table already exists) by executing the same query using pyodbc I don't see any errors returned:
def createSQLTable():
try:
sql_conn = pyodbc.connect('Driver={ODBC Driver 17 for SQL Server};'
'Server=[SERVERNAME];'
'Database=[DATABASENAME];'
'Trusted_Connection=yes;')
cursor = sql_conn.cursor()
sql = '''set nocount on;
use [DATABASENAME]
create table test(test1 varchar(5));'''
cursor.execute(sql)
except Exception as e:
print(e)
finally:
sql_conn.commit()
cursor.close()
sql_conn.close()
createSQLTable()
If I run the same TSQL on SQL Server I get the error message:
Msg 2714, Level 16, State 6, Line 1 There is already an object named 'test' in the database.
So how do I get Python/Pyodbc to throw the same or similar error?