我想在Python脚本中使用OPENJSON将一些基本的JSON导入SQL数据库。我最初尝试使用更复杂的JSON文件,但为了这篇文章而简化了它。下面是我有:pypyodbc:关键字“WITH”附近的OPENJSON语法不正确
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查询。
非常重要因为“OPENJSON”是SQL Server特有的方法,所以不需要其他SQL引擎。此外,错误似乎很清楚。您没有正确使用'WITH()'子句(通常保留给主查询中由名称引用的'SELECT'语句)。事实上,在这里包含可能是多余的。除此之外 - 在Management Studio中测试查询。 – Parfait