I could not find a way to run a T-SQL Script using pyodbc without having to change the script content.
Here's the simple SQL script file I'm trying to run:
SET NOCOUNT ON
USE db_test
GO
CREATE OR ALTER FUNCTION usf_test()
RETURNS NVARCHAR(10)
BEGIN
DECLARE @var NVARCHAR(10)
SELECT @var = 'TEST'
RETURN @var
END
and this is the python code I'm using:
cursor.execute(file_content)
cursor.fetchall()
This is the error message: pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near 'GO'. (102) (SQLExecDirectW)")
and yes, this runs with no errors directly on SSMS.
Is there a way to run a T-SQL script using pyodbc (or any other lib that supports SQL Server) in Python?
Notice I can't change the script content, so splitting it into multiple statements is not an option. I have to run the script just as it is on the file.