简体   繁体   English

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

[英]power query excel table mssql returns invalid column name

Here is my Customer Parameters table 这是我的客户参数表

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

And my Query 我的查询

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

I am setting Organization = Customer_Name but I keep getting the following 我正在设置Organization = Customer_Name但我继续得到以下内容

Message=Invalid column name 'Customer_Name'

Is there a way to accomplish this 有没有办法实现这一目标

Customer_Name is not a column in your SQL table, so it fails with that message. Customer_Name不是SQL表中的列,因此它失败并显示该消息。 If you want to use Customer_Name in the query, it needs to be added to the string. 如果要在查询中使用Customer_Name,则需要将其添加到字符串中。 This is done using the & operator. 这是使用&运算符完成的。 In your case, you would do something like 在你的情况下,你会做类似的事情

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

That will cause problems if the customer name has a quote in it (like O'Neill), so you could do 如果客户名称中有引号(如O'Neill),这将导致问题,所以你可以这样做

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

to try and escape quotes. 试图逃避报价。

Escaping quotes is not always sufficient, so if you're running this query on a database where outsiders can create customers you should avoid using the above method. 转义引号并不总是足够的,因此如果您在外部人员可以创建客户的数据库上运行此查询,则应避免使用上述方法。 If you filter the column by Customer_Name (through Table.SelectRows(tableName, each [Organization] = Customer_Name) ), you should get the same result without any security concerns. 如果按Customer_Name筛选列(通过Table.SelectRows(tableName, each [Organization] = Customer_Name) ),则应该得到相同的结果而不存在任何安全问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM