【问题标题】:power query excel table mssql returns invalid column name电源查询excel表mssql返回无效的列名
【发布时间】:2015-04-02 06:41:35
【问题描述】:

这是我的客户参数表

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'

有没有办法做到这一点

【问题讨论】:

    标签: sql-server powerquery


    【解决方案1】:

    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))过滤列,您应该得到相同的结果,而没有任何安全问题。

    【讨论】:

      猜你喜欢
      • 2018-10-28
      • 2017-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多