openpyjson包含错误的openpyjson关键字“near-odbc语法”

2024-04-26 20:59:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试在Python脚本中使用OPENJSON将一些基本JSON导入到SQL数据库中。我最初尝试使用一个更复杂的JSON文件,但为了本文的目的简化了它。以下是我所拥有的:

sql_statement = "declare @json nvarchar(max) = '{\"name\":\"James\"}'; SELECT * FROM OPENJSON(@json) WITH (name nvarchar(20))" 
cursor.execute(sql_statement)
cursor.commit()
connection.close()

我收到的错误:

pypyodbc.ProgrammingError: (u'42000', u"[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.")

你觉得我为什么会看到这个错误吗?我成功地使用相同的pypyodbc/数据库配置执行其他SQL查询。在


Tags: thename数据库jsonsqlserver错误with
1条回答
网友
1楼 · 发布于 2024-04-26 20:59:02

问题可能是您的数据库运行在较旧的兼容级别上,而openjson不可用。在

若要查找数据库的兼容级别,请运行以下SQL语句:

SELECT compatibility_level  FROM sys.databases WHERE name = 'your_db_name';

如果结果为120或更低,则需要通过运行以下命令将兼容级别更新到130:

^{pr2}$

注意:如果您的数据库实际上是azuresqldb,您也应该检查版本,因为openjson不适用于12.x之前的版本

相关问题 更多 >