1

我正在尝试在 Oracle 数据库中创建视图,但不断收到 ORA-00907 错误(缺少右括号)。我的SQL如下:

CREATE VIEW my_view AS 
(
SELECT metadata.ID,metadata.Field1,metadata.Field2,metadata.Field3,metadata.Field4,attribute1.StrValue AS Attr1, attribute2.StrValue AS Attr2 
FROM metadata,data AS attribute1,data AS attribute2 
WHERE 
    (
    metadata.Type = 'TYPE1'
    ) 
AND 
    (
    metadata.ID = attribute1.ID AND attribute1.name = 'attr1'
    ) 
    AND 
    (
    metadata.ID = attribute2.ID AND attribute2.name = 'attr2'
    )
)

表元数据定义实体,数据定义这些实体的属性。

这在 MS SQL 和 MySQL 中运行良好,但我不断从 Oracle 收到上述错误。

没有与 Oracle 合作太久,所以我不太了解它的怪癖。

4

4 回答 4

4
CREATE VIEW my_view AS 
(
SELECT  metadata.ID,metadata.Field1,metadata.Field2,
        metadata.Field3,metadata.Field4,
        attribute1.StrValue AS Attr1, attribute2.StrValue AS Attr2 
FROM    metadata, data /* No AS here */ attribute1,
        data /* No AS here */ attribute2 
WHERE 
        (
        metadata.Type = 'TYPE1'
        ) 
AND 
        (
        metadata.ID = attribute1.ID AND attribute1.name = 'attr1'
        ) 
        AND 
        (
        metadata.ID = attribute2.ID AND attribute2.name = 'attr2'
        )
)

我删除ASdataattribute1

于 2009-04-08T16:58:48.087 回答
3

既然你的括号都不是必需的,而且它们使阅读代码变得更加困难,为什么不去掉它们呢?

于 2009-04-08T17:00:57.413 回答
3

您需要删除 FROM 子句中的 AS。Oracle 允许使用可选的 AS 为列名加上别名,但不允许提供别名

SQL> ed
Wrote file afiedt.buf

  1  CREATE VIEW my_view AS
  2  (
  3  SELECT metadata.ID,metadata.Field1,metadata.Field2,metadata.Field3,metadata
.Field4,attribute1.StrValue AS Attr1, attribute2.StrValue AS Attr2
  4  FROM metadata,data attribute1,data attribute2
  5  WHERE
  6          (
  7          metadata.Type = 'TYPE1'
  8          )
  9  AND
 10          (
 11          metadata.ID = attribute1.ID AND attribute1.name = 'attr1'
 12          )
 13          AND
 14          (
 15          metadata.ID = attribute2.ID AND attribute2.name = 'attr2'
 16          )
 17* )
SQL> /

View created.

根据您使用的工具,知道 SQL*Plus 将准确地显示语法错误发生的位置可能很有用——下面的片段显示它反对 AS 关键字。

SQL> ed
Wrote file afiedt.buf

  1  CREATE VIEW my_view AS
  2  (
  3  SELECT metadata.ID,metadata.Field1,metadata.Field2,metadata.Field3,metadata
.Field4,attribute1.StrValue AS Attr1, attribute2.StrValue AS Attr2
  4  FROM metadata,data AS attribute1,data AS attribute2
  5  WHERE
  6          (
  7          metadata.Type = 'TYPE1'
  8          )
  9  AND
 10          (
 11          metadata.ID = attribute1.ID AND attribute1.name = 'attr1'
 12          )
 13          AND
 14          (
 15          metadata.ID = attribute2.ID AND attribute2.name = 'attr2'
 16          )
 17* )
SQL> /
FROM metadata,data AS attribute1,data AS attribute2
                   *
ERROR at line 4:
ORA-00907: missing right parenthesis
于 2009-04-08T17:03:52.990 回答
2

其他人指出您的 FROM 子句中的 AS 关键字是问题所在。我建议以下内容作为同一语句的更清晰、更易于阅读的版本:

create view my_view as 
select
  meta.id
 ,meta.field1
 ,meta.field2
 ,meta.field3
 ,meta.field4
 ,att1.strvalue as attr1
 ,att2.strvalue as attr2 
from
  metadata meta
 ,data att1
 ,data att2 
where meta.id = att1.id 
  and meta.id = att2.id 
  and meta.type = 'TYPE1'
  and att1.name = 'attr1'
  and att2.name = 'attr2'
于 2009-04-08T17:14:00.017 回答