sql-server - Incorrect syntax near Go with Pypyodbc
问题描述
I am using the pypyodbc
library to establish a connection to a SQL Server 2008 R2 database and every time I try to execute a .sql file I encounter the following error:
pypyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'Go'.")
This is the sql query I am trying to execute:
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
This is the python script that I am using:
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
The workaround for this problem is to delete the Use SL_Site1_App Go
line but I want to know if this is a known problem related to the pypyodbc library to process these lines and if so, where should I look to notify the developers about this issue.
解决方案
GO
is a batch separator used by sqlcmd
and SSMS. It's not a T-SQL operator.
Considering you're using an application to connect to SQL Server, declare your database in the connection string, by adding database="SL_Site1_App"
, and then remove the USE
and GO
statements in your SQL Statement.
推荐阅读
- javascript - 当我尝试应用动态 css 大小属性时,应用程序渲染大于视口
- php - 在 PHP 中添加被覆盖的超链接
- android - 我的 Java 函数中的 2 行不想在 Android 应用程序中执行
- java - 正则表达式查找具有条件的子字符串
- java - Domino 9 FP10 xpages 外部 JAR
- android - 即使在 EditText 获得焦点后也显示对话框全屏
- javascript - 如何从 SharePoint 连接到 OAuth
- javascript - react-native-datepicker 中的当前日期值为空
- prolog - 为什么你在prolog中定义了两次规则
- ruby-on-rails - 如何在 Rails 6 中将 i18n-js 与 webpacker 一起使用?