简体   繁体   中英

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

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. If you want to use Customer_Name in the query, it needs to be added to the string. 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

"...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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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