创建视图时Oracle抛出SQL错误

时间:2009-04-08 16:55:46

标签: sql oracle ora-00907

我正在尝试在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 个答案:

答案 0 :(得分: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'
        )
)

我在ASdata

之间删除了attribute1

答案 1 :(得分:3)

既然你的括号都没有必要,并且他们更难以阅读代码,为什么不把它们除掉呢?

答案 2 :(得分: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

答案 3 :(得分: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'