0

这是我的客户参数表

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'

有没有办法做到这一点

4

1 回答 1

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

于 2015-04-01T22:41:35.670 回答