9

在 Microsoft SQL Server 2000 上执行以下(完整)SQL 查询时:

SELECT B.ARTIFACTTNS, B.ARTIFACTNAME, B.ARTIFACTTYPE, B.INITIALBYTES, B.TIMESTAMP1, B.FILENAME, B.BACKINGCLASS, 
       B.CHARENCODING, B.APPNAME, B.COMPONENTTNS, B.COMPONENTNAME, B.SCAMODULENAME, B.SCACOMPONENTNAME 
FROM (SELECT DISTINCT A.ARTIFACTTYPE, A.ARTIFACTTNS, A.ARTIFACTNAME 
      FROM (SELECT DISTINCT ARTIFACTTYPE, ARTIFACTTNS, ARTIFACTNAME 
            FROM CUSTPROPERTIES WHERE PNAME = 'AcmeSystemName' AND PVALUE = 'MyRuleGroup' 
                  UNION SELECT DISTINCT ARTIFACTTYPE, ARTIFACTTNS, ARTIFACTNAME 
                          FROM CUSTPROPERTIES WHERE PNAME = 'AcmeSystemDisplayName' AND PVALUE = 'MyRuleGroup') A, 
           (SELECT DISTINCT ARTIFACTTYPE, ARTIFACTTNS, ARTIFACTNAME 
            FROM CUSTPROPERTIES WHERE PNAME = 'AcmeSystemTargetNameSpace' AND PVALUE = 'http://MyModule') B 
WHERE A.ARTIFACTTYPE = B.ARTIFACTTYPE AND A.ARTIFACTTNS = B.ARTIFACTTNS AND A.ARTIFACTNAME = B.ARTIFACTNAME) A, BYTESTORE B 
    WHERE (A.ARTIFACTTYPE = 'BRG') AND A.ARTIFACTTYPE = B.ARTIFACTTYPE AND A.ARTIFACTTNS = B.ARTIFACTTNS AND A.ARTIFACTNAME = B.ARTIFACTNAME 
    ORDER BY ARTIFACTTYPE, ARTIFACTTNS, ARTIFACTNAME

我得到以下异常:

java.sql.SQLException: [Acme][SQLServer JDBC Driver][SQLServer]
    Ambiguous column name 'ARTIFACTTYPE'.

我在这里做错了什么,我该如何纠正?

4

5 回答 5

25

因为ARTIFACTTYPE可以引用A.ARTIFACTTYPEorB.ARTIFACTTYPE并且服务器需要知道您想要哪个,只需将其更改为A.ARTIFACTTYPE,在这种情况下您应该没问题。

为了澄清,您需要在列名不明确时指定别名前缀。始终使用别名前缀并不是一个坏习惯,因为它可以在您阅读查询时清楚地说明哪些列来自哪些表,并消除了类似这样的问题。

有人可能想知道,当它们都引用同一个表中的同一列时,为什么需要区分您想要的两列中的哪一列。答案是,当您将表连接到自身时,A.column 和 B.column 中的值可能会有所不同,具体取决于连接条件(例如外部连接的情况,其中一列中的值可能是空值)。

于 2008-11-25T16:54:10.070 回答
2

如果那是您正在运行的确切查询,我不知道为什么它会发现任何模棱两可的东西。

我编写了我认为是等效查询的内容,并在我的数据库(Oracle)中毫无问题地运行它。

编辑在 Oracle 中添加新实验的精确输出。本实验中执行的查询是 OP 给出的确切查询,并填写了表名。 NO OTHER CHANGES。这个查询没有任何歧义。因此,要么这不是正在执行的确切查询,要么 SQL Server 存在解析器错误。

SQL> create table props (pname varchar2(100),
  2                       pvalue varchar2(100),
  3                       artifacttype number,
  4                       artifacttns number,
  5                       artifactname number);

Table created.

SQL> SELECT      
  2    DISTINCT A.ARTIFACTTYPE, A.ARTIFACTTNS, A.ARTIFACTNAME
  3  FROM
  4   (SELECT DISTINCT 
  5      ARTIFACTTYPE, 
  6      ARTIFACTTNS, 
  7      ARTIFACTNAME 
  8    FROM props 
  9    WHERE PNAME = 'AcmeSystemName' 
 10        AND PVALUE = 'MyRuleGroup' 
 11    UNION 
 12    SELECT DISTINCT 
 13      ARTIFACTTYPE, 
 14      ARTIFACTTNS, 
 15      ARTIFACTNAME 
 16    FROM props
 17    WHERE PNAME = 'AcmeSystemDisplayName' 
 18        AND PVALUE = 'MyRuleGroup') A, 
 19  (SELECT DISTINCT 
 20      ARTIFACTTYPE, 
 21      ARTIFACTTNS, 
 22      ARTIFACTNAME 
 23   FROM props 
 24   WHERE PNAME = 'AcmeSystemTargetNameSpace' 
 25      AND PVALUE = 'http://mymodule') B
 26  WHERE A.ARTIFACTTYPE = B.ARTIFACTTYPE 
 27      AND A.ARTIFACTTNS = B.ARTIFACTTNS 
 28      AND A.ARTIFACTNAME = B.ARTIFACTNAME
 29  /

no rows selected

结束编辑

我对解决该错误的建议是为每个 select 子句中的表提供一个唯一别名并限定所有列引用。像这样:

SELECT
  DISTINCT A.ARTIFACTTYPE, A.ARTIFACTTNS, A.ARTIFACTNAME
FROM
 (SELECT DISTINCT 
    P1.ARTIFACTTYPE, 
    P1.ARTIFACTTNS, 
    P1.ARTIFACTNAME 
  FROM {PROPERTIES_TABLE_NAME} P1
  WHERE PNAME = 'AcmeSystemName' 
      AND PVALUE = 'MyRuleGroup' 
  UNION 
  SELECT DISTINCT 
    P2.ARTIFACTTYPE, 
    P2.ARTIFACTTNS, 
    P2.ARTIFACTNAME 
  FROM {PROPERTIES_TABLE_NAME} P2
  WHERE PNAME = 'AcmeSystemDisplayName' 
      AND PVALUE = 'MyRuleGroup') A, 
(SELECT DISTINCT 
    P3.ARTIFACTTYPE, 
    P3.ARTIFACTTNS, 
    P3.ARTIFACTNAME 
 FROM {PROPERTIES_TABLE_NAME} P3
 WHERE PNAME = 'AcmeSystemTargetNameSpace' 
    AND PVALUE = 'http://mymodule') B
WHERE A.ARTIFACTTYPE = B.ARTIFACTTYPE 
    AND A.ARTIFACTTNS = B.ARTIFACTTNS 
    AND A.ARTIFACTNAME = B.ARTIFACTNAME
于 2008-11-25T17:54:46.170 回答
1

您是否列出了完整的查询?也许您还有 ORDER BY 子句 - 这可能会导致该问题

我会支持戴夫发布的查询应该没有问题

于 2008-11-25T18:14:52.153 回答
0

需要明确的是,第 13、14 和 15 行的列不明确。

于 2008-11-25T17:42:38.177 回答
0

您需要在ORDER BY子句中指定表,如下所示:

ORDER BY A.ARTIFACTTYPE, A.ARTIFACTTNS, A.ARTIFACTNAME
于 2009-01-09T18:36:29.337 回答