首页 > 解决方案 > 在excel的odbc power query中使用sql字符串作为sql语句

问题描述

我希望使用一个字符串,该字符串从我的单元格中获取一些参数并将其用作我的 sql 语句,如下所示:

" select * from table where " & data_from_cells & " group by ...;"

将其存储为 sqlstring

let
mystring = sqlstring,
myQuery = Odbc.Query("driver={Oracle in etc etc etc", mystring)

我遇到了这个错误

Formula.Firewall: Query '...' (step 'myQuery') references other queries or steps, so it may not directly access a data source. Please rebuild this data combination.

现在显然我不能将外部查询和另一个查询结合起来——但我只使用 Excel 中的传递参数作为我的 sql 字符串?我也希望使用WITH关键字来使用参数进行嵌套查询,但它甚至不允许我将 excel 中的值与 sql 语句结合起来。

需要明确的是,data_from_cells被转换并格式化为字符串。

标签: excelpowerbipowerquery

解决方案


当执行其他操作的其他查询调用执行其他操作的查询时,有时您会遇到防火墙问题。

解决这个问题的方法是在一个查询中完成所有事情。

解决这个问题而不会产生可怕的代码的方法是将调用的查询返回结果更改为返回结果 函数

对于 sqlstring:

() => " select * from table where " & data_from_cells & " group by ...;" // returns a function that gets the query string when called

那么你的“myQuery”查询可以是

let
mystring = sqlstring(), //note the parentheses!
myQuery = Odbc.Query("driver={Oracle in etc etc etc", mystring)

推荐阅读