2
import pyodbc
con = pyodbc.connect(connection_string) #connection_string already defined. con works
cur = con.cursor()
cur.execute("CREATE TABLE [schema].[TestTestTest](testcolumn1 int, testcolumn2 int)")
con.commit() #I've also tried cur.commit()

The error points to line 4 with the description:

ProgrammingError: ('42000', '[42000] [Microsoft][SQL Server Native Client 11.0][SQL Server]111212;Operation cannot be performed within a transaction. (111212) (SQLExecDirectW)')

Any ideas? Can we not create tables with pyodbc?

user7890887
  • 21
  • 1
  • 2
  • Could be related to this... http://stackoverflow.com/questions/42006192/create-database-statement-not-allowed-within-multi-statement-transaction-when – SS_DBA Apr 19 '17 at 16:02
  • Possible duplicate of ["CREATE DATABASE statement not allowed within multi-statement transaction" when using pyodbc](http://stackoverflow.com/questions/42006192/create-database-statement-not-allowed-within-multi-statement-transaction-when) – Gord Thompson Apr 19 '17 at 23:35
  • Setting `con.autocommit = True` as the post that Gord and Wei linked to suggests did not help me when trying to create a table. I no longer got the error but no table was created. Did you resolve this? – Vito Feb 22 '18 at 21:33

1 Answers1

2
cur.execute('commit')

use the above comment, which will resolve your issue