Go with Pypyodbc 附近的语法不正确

Incorrect syntax near Go with Pypyodbc

我正在使用 pypyodbc 库建立与 SQL Server 2008 R2 数据库的连接,每次我尝试执行 .sql 文件时都会遇到以下错误:

pypyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'Go'.")

这是我要执行的 sql 查询:

Use SL_Site1_App
Go

select emp_num,name, trans_num, job, trans_type
from Hours where trans_type like '1000%'  order by trans_date desc

这是我正在使用的 python 脚本:

import pypyodbc, ExcelFile

def main():
    # read the SQL queries externally
    queries = ['C:\Temp\Ready_to_use_queries\Connection_sql_python.sql']

    for index, query in enumerate(queries):
        cursor = initiate_connection_db()
        results = retrieve_results_query(cursor, query)
        if index == 0:
            ExcelFile.write_to_workbook(results)
            print("The workbook has been created and data has been inserted.\n")

def initiate_connection_db():      
    connection_live_db = pypyodbc.connect(driver="{SQL Server}", server="xxx.xxx.xxx.xxx", uid="my-name",
                                      pwd="try-and-guess", Trusted_Connection="No")
    connection = connection_live_db.cursor()
    return connection

此问题的解决方法是删除 Use SL_Site1_App Go 行,但我想知道这是否是与 pypyodbc 库处理这些行相关的已知问题,如果是,我应该在哪里通知关于这个问题的开发者。

GOsqlcmd 和 SSMS 使用的批处理分隔符。它不是 T-SQL 运算符。

考虑到您正在使用应用程序连接到 SQL 服务器,通过添加 database="SL_Site1_App" 在连接字符串中声明您的数据库,然后删除 USEGO 语句在你的 SQL 语句中。