问题
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:
- Is it possible to execute the query the way I'm doing it?
- 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
回答1:
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.
来源:https://stackoverflow.com/questions/30533508/how-to-execute-an-sql-file-in-pymssql