Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute an .sql file in pymssql

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.

I'm trying to open the file in memory and execute the content:

file = open(options.sqlFile, 'r')
sqlFileContents = file.read()
file.close()

cursor.execute(sqlFileContents)
conn.commit()

But it's returning me errors:

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")

So basically I have two questions:

  1. Is it possible to execute the query the way I'm doing it?
  2. is the sql query file itself the problem?

Thanks for any help.

EDIT: Here is the SQL:

Here is the SQL:

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<>0 AND @@TRANCOUNT>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>0 BEGIN
PRINT 'The database update succeeded'
COMMIT TRANSACTION
END
ELSE PRINT 'The database update failed'
GO
DROP TABLE #tmpErrors
GO
like image 584
fragk Avatar asked May 29 '15 15:05

fragk


People also ask

How do I run a .SQL file in SQL server?

Click Query > Connection > Connect to connect to the server that contains the database you want to access. Select the appropriate StarTeam Server database. Open the tuning script, by choosing File > Open > foldername\scriptname. Execute the script, by clicking the Execute button on the toolbar or by pressing F5.


1 Answers

Yes it is possible to do it like that. I often do so, it is cleaner that hardcoding the SQL in your code.

Can you add the SQL to your post? there's probably a messed up character somewhere.

I do it like that (with pyodbc):

with open('%smysql.sql' % SQL_DIR) as f: 
            sql = f.read() % params # Don't do that with untrusted inputs
            cursor.execute(sql)
            cursor.commit()
            cursor.close()

EDIT: Remove all the GO it's not an real SQL statement. Check this answer: Using "GO" within a transaction.

Then it should be fine.

like image 105
Maresh Avatar answered Oct 05 '22 23:10

Maresh