gpt4 book ai didi

python - 如何使用 azureml-dataprep 运行带有WITH 子句的 SQL 查询?

转载 作者:行者123 更新时间:2023-12-02 06:26:45 27 4
gpt4 key购买 nike

我从同事那里继承了一些 SQL 查询,这些查询大量使用了针对 MS SQL 数据库的 SQL WITH 子句,但我似乎无法运行它们并使用 azureml-dataprep 以 pandas 数据帧的形式获取结果.

作为一个简单的例子,考虑例如代码

import azureml.dataprep as dprep
import time

secret = dprep.register_secret(value="flowerpower", id="id")

db = dprep.MSSQLDataSource(server_name="myserver.net, 12345",
database_name="Flowers",
user_name="davide",
password=secret)

然后我定义两个查询,它们应该完全等效:

flower_query = '''SELECT TOP (100) [Sepal.Length], [Sepal.Width]
FROM [Flowers].[Iris]'''

# unnecessarily convoluted, but still perfectly legit syntax:
flower_with_query = '''WITH I AS (SELECT TOP (100) [Sepal.Length], [Sepal.Width]
FROM [Flowers].[Iris])
SELECT * FROM I'''

以下“非WITH”查询运行没有问题:

flower_dataflow = dprep.read_sql(db, flower_query)
flower_df = flower_dataflow.to_pandas_dataframe()

WITH 变体

flower_dataflow = dprep.read_sql(db, flower_with_query) # using the WITH query
flower_df = flower_dataflow.to_pandas_dataframe()

失败

ExecutionError: Error Code: ScriptExecution.DatabaseQuery FailedStep: b1da84ed-e429-4752-ac98-4cb74b9ca114 Error Message:ScriptExecutionException was caused by DatabaseQueryException.
'MSSQL' encountered exception of with error code '156' while executingquery 'SELECT * FROM (WITH I AS (SELECT TOP (100) [Sepal.Length],[Sepal.Width]FROM [Flowers].[Iris]) SELECT * FROM I) assubquery'. SqlException class: 15. HResult: x80131904.Incorrect syntax near the keyword 'WITH'. Incorrect syntax near the keyword 'with'. If this statement is a common table expression, anxmlnamespaces clause or a change tracking context clause, the previousstatement must be terminated with a semicolon. Incorrect syntax near')'.

从错误来看,库似乎在我的原始查询(?)前面添加了 SELECT * FROM,这会引起问题。
在我的 ; 前面加上分号 flower_with_query 不足以解决问题,这样做时我得到

'MSSQL' encountered exception of with error code '102' while executing query 'SELECT * FROM (;WITH I AS (SELECT TOP (100)...

可通过 azureml-dataprep 运行的查询集是否有限制?如果是这样,是否有一种解决方法可以在不进行任何 SQL 重构的情况下成功运行查询?我知道 ODBC 驱动程序,但我宁愿使用需要最少配置的解决方案(我认为 azureml-dataprep 在这方面表现出色)。

我正在使用 azureml-dataprep-2.0.7azureml-core-1.13.0

最佳答案

关于python - 如何使用 azureml-dataprep 运行带有WITH 子句的 SQL 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63774273/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com