简体   繁体   English

来自OpenQuery结果的SQL语句中的“无效的列名”错误

[英]“Invalid column name” error on SQL statement from OpenQuery results

I'm trying to perform a SQL query through a linked SSAS server. 我正在尝试通过链接的SSAS服务器执行SQL查询。 The initial query works fine: 初始查询工作正常:

SELECT "Ugly OLAP name" as "Value" 
FROM OpenQuery( OLAP, 'OLAP Query')

But if I try to add: 但是,如果我尝试添加:

WHERE "Value" > 0

I get an error 我收到一个错误

Invalid column name 'Value' 列名称'Value'无效

Any ideas what I might be doing wrong? 我有什么想法可能做错了吗?


So the problem was that the order in which elements of the query are processed are different that the order they are written. 所以问题是处理查询元素的顺序与编写它们的顺序不同。 According to this source: 根据这个消息来源:

http://blogs.x2line.com/al/archive/2007/06/30/3187.aspx http://blogs.x2line.com/al/archive/2007/06/30/3187.aspx

The order of evaluation in MSSQL is: MSSQL中的评估顺序是:

  1. FROM
  2. ON
  3. JOIN 加入
  4. WHERE 哪里
  5. GROUP BY 通过...分组
  6. HAVING HAVING
  7. SELECT 选择
  8. ORDER BY 订购

So the alias wasn't processed until after the WHERE and HAVING clauses. 因此,在WHERE和HAVING子句之后才会处理别名。

This should work: 这应该工作:

SELECT A.Value
FROM (
SELECT "Ugly OLAP name" as "Value" 
FROM OpenQuery( OLAP, 'OLAP Query')
) AS a
WHERE a.Value > 0

It's not that Value is a reserved word, the problem is that it's a column alias, not the column name. 并不是Value是一个保留字,问题是它是列别名,而不是列名。 By making it an inline view, "Value" becomes the column name and can then be used in a where clause. 通过使其成为内联视图,“Value”成为列名,然后可以在where子句中使用。

You're using "Value" as a column alias, and I don't think the alias can appear in the where clause. 您使用“Value”作为列别名,我不认为别名可以出现在where子句中。 It's simply used to name the returned column value. 它只是用于命名返回的列值。 Your where clause should refer to the original column name: 您的where子句应该引用原始列名:

SELECT "Ugly OLAP name" as "Value" 
FROM OpenQuery( OLAP, 'OLAP Query')
WHERE "Ugly OLAP name" > 0

I can vouch for leaving it out of GROUP BY. 我可以保证将它从GROUP BY中删除。 Good news is, it works just fine being a plain old selected alias. 好消息是,它可以很好地成为一个简单的旧选择别名。

Oh, bummer. 哦,真可惜。 I just saw, you select AS FOO. 我刚刚看到,你选择AS FOO。 Don't you need a HAVING claus in this case? 在这种情况下你不需要HAVING克劳斯吗?

SELECT whatever AS value FROM table HAVING value > 1;

I still would not use "value". 我仍然不会使用“价值”。 But to be sure, look it up in your docs! 但是可以肯定的是,在你的文档中查找它!

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

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