1

I am unable to create a SQL Server stored procedure using Python's pyodbc. The command executes correctly and I get no error message however the stored procedure does not appear on the server

import pyodbc

host = 'myServer'
database = 'model'
conn = pyodbc.connect(
        r'DRIVER={SQL Server Native Client 11.0};' +
        r'SERVER=' + host + ';' +
        r'DATABASE=' + database + ';' +
        r'Trusted_Connection=yes'
    )
cursor = conn.cursor()

sql = """
        CREATE OR ALTER PROCEDURE [dbo].[Test] AS 
        SELECT 1  
        """
cursor.execute(sql)
conn.close()
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
michal111
  • 400
  • 4
  • 18

1 Answers1

1

pyodbc connections default to having autocommit disabled as specified in Python's DB API 2.0 spec. In that mode, any changes to the database must be committed by calling commit() on the Connection.

If you want a connection with autocommit enabled, see this answer for details.

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418