如何使用 Power Query M 将 Excel table 列值拉入 SQL WHERE 子句?
How do I pull Excel table column values into a SQL WHERE clause using Power Query M?
我有一个 Excel table,最终用户在其中的 "RefNum" 列中输入参考编号。我还创建了一个 "RefNum_SQL" 列,其中包含一个公式,该公式会自动为 WHERE 子句设置带有逗号和括号的列表格式,以防 SQL/M 部分更容易。我们将此 table 称为 "Input" table:
RefNum RefNum_Formatted
2123 (2123,
2456 2456,
2789 2789)
然后我有一个 SQL 查询,它提取与这些参考号相关的其他详细信息,并将它们加载到 Excel 工作簿的另一个 sheet 上的 table 中。
SELECT RefNum, LocationID, ShipDate
FROM database.dbo.Products (NOLOCK)
WHERE RefNum IN (2123, 2456, 2789)
然后将查询结果加载到工作簿中另一个 sheet 上的 table,我们将其称为 "Output" table:
RefNum LocationID ShipDate
2123 13321 12/3/2019
2456 16654 5/17/2019
2789 19987 8/24/2019
Power Query M 代码能否将 RefNum 列中的值直接拉入 SQL 查询的 WHERE 子句中,以便最终用户不必手动调整查询中的查询列表更改时的高级编辑器?
这是我根据目前找到的资源尝试编写的代码:
let
Source = Sql.Database("server", "database")
RefNum_Formatted = table.Column(Input, RefNum_Formatted)
Output = Value.NativeQuery( Source,
"SELECT RefNum, LocationID, ShipDate
FROM database.dbo.Products (NOLOCK)
WHERE RefNum = @RefNum_Formatted")
in
Output
当尝试 运行 上面的 M 代码时,我收到以下错误:
DataSource.Error: Microsoft SQL: Must declare the scalar variable "@RefNum_Formatted".
我确实理解,正如目前所写的那样,Power Query 期望 @RefNum_Formatted 是 SQL 中声明的变量,而不是 Excel table 中的声明变量;但是,我不知道如何创建与可以使用 M 拉入查询的 "Input" table 值的连接。可以做到吗?
下面的链接中提出了类似的问题,但我无法成功应用任何答案:
这里有几处不正确的地方
- "@RefNum_Formatted" 只是一个字符串而不是对
上一步
- RefNum_Formatted 步骤很可能是一个列表而不是
比字符串
尝试此代码(您可能需要调整 table/column 引用)
let
Source = Sql.Database("server", "database"),
RefNum_Formatted = List.Accumulate(Input[RefNum_Formatted],"",(state, current) => state & current), //List.Accumulate converts list to string
SQL_Code = "SELECT RefNum, LocationID, ShipDate
FROM database.dbo.Products (NOLOCK)
WHERE RefNum IN " & RefNum_Formatted, //that's how you pass a reference to a step, it cannot be done inside a string
Output = Value.NativeQuery( Source, SQL_Code)
in
Output
编辑:添加了源步骤
我有一个 Excel table,最终用户在其中的 "RefNum" 列中输入参考编号。我还创建了一个 "RefNum_SQL" 列,其中包含一个公式,该公式会自动为 WHERE 子句设置带有逗号和括号的列表格式,以防 SQL/M 部分更容易。我们将此 table 称为 "Input" table:
RefNum RefNum_Formatted
2123 (2123,
2456 2456,
2789 2789)
然后我有一个 SQL 查询,它提取与这些参考号相关的其他详细信息,并将它们加载到 Excel 工作簿的另一个 sheet 上的 table 中。
SELECT RefNum, LocationID, ShipDate
FROM database.dbo.Products (NOLOCK)
WHERE RefNum IN (2123, 2456, 2789)
然后将查询结果加载到工作簿中另一个 sheet 上的 table,我们将其称为 "Output" table:
RefNum LocationID ShipDate
2123 13321 12/3/2019
2456 16654 5/17/2019
2789 19987 8/24/2019
Power Query M 代码能否将 RefNum 列中的值直接拉入 SQL 查询的 WHERE 子句中,以便最终用户不必手动调整查询中的查询列表更改时的高级编辑器?
这是我根据目前找到的资源尝试编写的代码:
let
Source = Sql.Database("server", "database")
RefNum_Formatted = table.Column(Input, RefNum_Formatted)
Output = Value.NativeQuery( Source,
"SELECT RefNum, LocationID, ShipDate
FROM database.dbo.Products (NOLOCK)
WHERE RefNum = @RefNum_Formatted")
in
Output
当尝试 运行 上面的 M 代码时,我收到以下错误:
DataSource.Error: Microsoft SQL: Must declare the scalar variable "@RefNum_Formatted".
我确实理解,正如目前所写的那样,Power Query 期望 @RefNum_Formatted 是 SQL 中声明的变量,而不是 Excel table 中的声明变量;但是,我不知道如何创建与可以使用 M 拉入查询的 "Input" table 值的连接。可以做到吗?
下面的链接中提出了类似的问题,但我无法成功应用任何答案:
这里有几处不正确的地方
- "@RefNum_Formatted" 只是一个字符串而不是对 上一步
- RefNum_Formatted 步骤很可能是一个列表而不是 比字符串
尝试此代码(您可能需要调整 table/column 引用)
let
Source = Sql.Database("server", "database"),
RefNum_Formatted = List.Accumulate(Input[RefNum_Formatted],"",(state, current) => state & current), //List.Accumulate converts list to string
SQL_Code = "SELECT RefNum, LocationID, ShipDate
FROM database.dbo.Products (NOLOCK)
WHERE RefNum IN " & RefNum_Formatted, //that's how you pass a reference to a step, it cannot be done inside a string
Output = Value.NativeQuery( Source, SQL_Code)
in
Output
编辑:添加了源步骤