title: How to execute an .sql file in pymssql
body: <p>I'm trying to execute an sql file in python using pymssql, this file contains a BEGIN TRANSACTION, a COMMIT TRANSACTION and an END, and some safety nets before and after.</p>
  
  <p>I'm trying to open the file in memory and execute the content:</p>
  
  <pre><code>file = open(options.sqlFile, 'r')
  sqlFileContents = file.read()
  file.close()
  
  cursor.execute(sqlFileContents)
  conn.commit()
  </code></pre>
  
  <p>But it's returning me errors:</p>
  
  <pre><code>pymssql.ProgrammingError: (102, "Incorrect syntax near 'GO'.DB-Lib error message 102, severity 15:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 102, severity 15:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 102, severity 1
  5:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 102, severity 15:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 102, severity 15:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 102, severity     
  15:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 102, severity 15:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 102, severity 15:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 102,severity 
  15:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 102, severity 15:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 102, severity 15:\nGeneral SQL Server error: Check messages from the SQL Server\n")
  </code></pre>
  
  <p>So basically I have two questions:</p>
  
  <ol>
  <li>Is it possible to execute the query the way I'm doing it?</li>
  <li>is the sql query file itself the problem?</li>
  </ol>
  
  <p>Thanks for any help.</p>
  
  <p>EDIT: Here is the SQL:</p>
  
  <p>Here is the SQL:</p>
  
  <pre><code>SET NUMERIC_ROUNDABORT OFF
  GO
  SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT,             QUOTED_IDENTIFIER, ANSI_NULLS ON
  GO
  IF EXISTS (SELECT * FROM tempdb..sysobjects WHERE     id=OBJECT_ID('tempdb..#tmpErrors')) DROP TABLE #tmpErrors
  GO
  CREATE TABLE #tmpErrors (Error int)
  GO
  SET XACT_ABORT ON
  GO
  SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
  GO
  BEGIN TRANSACTION
  PRINT N'Adding Release Version to [admin].[ReleaseHistory]'
  GO
  INSERT INTO [admin].[ReleaseHistory] VALUES (GetUTCDate(), '1.7')
  GO
  IF @@ERROR&lt;&gt;0 AND @@TRANCOUNT&gt;0 ROLLBACK TRANSACTION
  GO
  IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN     TRANSACTION END
  GO
  IF EXISTS (SELECT * FROM #tmpErrors) ROLLBACK TRANSACTION
  GO
  IF @@TRANCOUNT&gt;0 BEGIN
  PRINT 'The database update succeeded'
  COMMIT TRANSACTION
  END
  ELSE PRINT 'The database update failed'
  GO
  DROP TABLE #tmpErrors
  GO
  </code></pre>