power query excel table mssql返回无效的列名

时间:2015-02-25 01:24:11

标签: sql-server powerquery

这是我的客户参数表

let
  Source = Excel.CurrentWorkbook(){[Name="Customer"]}[Content],
  Customer1 = Source{0}[Customer]
in
  Customer1

我的查询

let
  Customer = Excel.CurrentWorkbook(){[Name="Customer"]}[Content],
  Customer_Name = Customer{0}[Customer],
  Source = Sql.Database("SERVER", "DATABASE", [Query="SELECT i.PriorityType as 'PRIORITY','COUNT' = COUNT(i.IncidentID)#(lf)FROM ININ_ISupport_S_Incident_Search2 (NULL) i#(lf)WHERE IncidentType IN ('Customer Support','Managed Services')#(lf)AND Organization = Customer_Name#(lf)AND IsResolved = 0#(lf)AND Active = 1#(lf)AND StateType = 'Open'#(lf)GROUP BY i.PriorityType#(lf)ORDER BY CASE#(lf) WHEN i.PriorityType = 'Code Red' THEN 1#(lf) WHEN i.PriorityType = 'Code Red RCA' THEN 2#(lf) WHEN i.PriorityType = 'High' THEN 3#(lf) WHEN i.PriorityType = 'Medium' THEN 4#(lf) WHEN i.PriorityType = 'Low' THEN 5#(lf)END ASC"])
in
  Source

我正在设置Organization = Customer_Name,但我不断收到以下内容

Message=Invalid column name 'Customer_Name'

有没有办法完成这个

1 个答案:

答案 0 :(得分:0)

Customer_Name不是SQL表中的列,因此它会失败并显示该消息。如果要在查询中使用Customer_Name,则需要将其添加到字符串中。这是使用&运营商。在你的情况下,你会做类似

的事情

"...AND Organization = '" & Customer_Name & "'#(lf)AND IsResolved...

如果客户名称中有引号(如O'Neill),那将导致问题,所以你可以这样做

"...AND Organization = '" & Text.Replace(Customer_Name, "'", "''") & "'#(lf)AND IsResolved...

试图逃避报价。

转义引号并不总是足够的,因此如果您在外人可以创建客户的数据库上运行此查询,则应避免使用上述方法。如果您按Customer_Name(通过Table.SelectRows(tableName, each [Organization] = Customer_Name))过滤列,则应获得相同的结果而不存在任何安全问题。