【问题标题】:Syntax to escape a reserved word in Oracle while using abbreviated table names使用缩写表名时在 Oracle 中转义保留字的语法
【发布时间】:2019-04-03 19:31:16
【问题描述】:

我有一个名为 "time_recs" 的表的 Oracle 11g 数据库。我需要在 SQL Developer 中运行一个查询,包括一个名为 comment 的列。 由于 comment 是保留字,我需要转义它,没关系。问题是当您使用缩写表名时,我找不到正确的语法。

例如:如果我运行 select "cmets" from "time_recs"; 效果很好。

但我需要运行的报告粘贴在下面。如果您看到第 17 行(“tr”.“comment”),我已经尝试了很多不同的东西:引号、双引号等。我仍然无法使其工作,因为我不断收到“无效标识符”错误

任何帮助将不胜感激。

select 
tr.record_date as "Date"
  , ua4.string_value as "Employee Number"
  , case when p.id_parent = 'root' then p.pname else pp.pname end as "Parent Project Name"
  , p.pname as "Project"
  , ct.pname as "Work Package"
  , tra1.string_value as "Cost Center"
  , ua1.string_value as "Discipline"
  , case when ua3.string_value = 'Research' then 'R' else 'D' end as "R or D" 
  , 'N/A' as "Sub Discipline"
  , tra2.string_value as "Primary Indication"
  , tra3.string_value as "Project Phase"
  , pa1.string_value as "Project Template Type"
  , pa2.string_value as "Secondary Indication"
  , pa3.string_value as "Therapeutic Area"
  , sum(tr.time_amount) as "Hours"
  , "tr"."comment"

from time_recs tr
  left join users us on
    tr.id_user = us.id_user
  left join users_attribs ua1 on 
    tr.id_user = ua1.id_user
  left join projects p on
    tr.id_project = p.id_project
  join projects pp on
    pp.id_project = p.id_parent
  join codes_tasks ct on 
    ct.id_code = tr.id_code_task
  left join time_recs_attribs tra1 on
     tr.id_time_rec = tra1.id_time_rec
  join attribute_types attr1 on
    attr1.id_attr_type = tra1.id_attr_type and attr1.pname = 'Cost Center'       
  join attribute_types atua2 on
    atua2.id_attr_type = ua1.id_attr_type and atua2.pname = 'Discipline'
  left join users_attribs ua3 on
    tr.id_user = ua3.id_user
  join attribute_types atua3 on
    atua3.id_attr_type = ua3.id_attr_type and atua3.pname = 'Organization'
  left join users_attribs ua4 on 
    tr.id_user = ua4.id_user and ua4.id_attr_type='D00C686107154F3FB2B97F47B172CB7F' --Employe Number
  left join time_recs_attribs tra2 on
    tr.id_time_rec  = tra2.id_time_rec 
  join attribute_types attr2 on
    attr2.id_attr_type = tra2.id_attr_type and attr2.pname = 'Primary Indication' 
  left join time_recs_attribs tra3 on
    tr.ID_TIME_REC = tra3.ID_TIME_REC
  join attribute_types attr3 on
    attr3.id_attr_type = tra3.id_attr_type and attr3.pname = 'Project Phase' 
  right join projects_attribs pa1 on
   tr.id_project = pa1.id_project 
  join attribute_types atpa1 on
    atpa1.id_attr_type = pa1.id_attr_type and atpa1.pname = 'Project Template Type'   
  left join projects_attribs pa2 on
    tr.id_project = pa2.id_project
  join attribute_types atpa2 on
    atpa2.id_attr_type = pa2.id_attr_type and atpa2.pname = 'Secondary Indication'
  left join projects_attribs pa3 on
   tr.id_project = pa3.id_project
  join attribute_types atpa3 on
    atpa3.id_attr_type = pa3.id_attr_type and atpa3.pname = 'Therapeutic Area'

where
  tr.record_date >= 20190101
  and tr.record_date <= 20190131
  having sum(tr.time_amount) >0

group by
tr.record_date
  , ua4.string_value
  , case when p.id_parent = 'root' then p.pname else pp.pname end
  , p.pname
  , ct.pname
  , tra1.string_value
  , ua1.string_value
  , case when ua3.string_value = 'Research' then 'R' else 'D' end
  , 'N/A'
  , tra2.string_value
  , tra3.string_value
  , pa1.string_value
  , pa2.string_value
  , pa3.string_value
  ;

【问题讨论】:

  • COMMENT 是保留字,"comment" 不是。是哪个?

标签: oracle oracle-sqldeveloper reserved-words


【解决方案1】:

你不需要任何东西按原样使用别名

SQL> create table time_recs ("comment" varchar2(10));

Table created.

SQL> insert into time_recs ("comment") values ('Littlefoot');

1 row created.

SQL> select tr."comment"          --> here
  2  from time_recs tr;

comment
----------
Littlefoot

SQL>

【讨论】:

  • 我的猜测是他的实际表中的列是大写的,并且由于保留字而只会给他错误。这是正确答案,但他的表格实际上可能要求他使用 tr."COMMENT"。
  • 我不这么认为,@Patrick。如果列名是大写的,那么您不必将其括在双引号中,因为 Oracle - 默认情况下 - 以大写形式创建列名。在这种情况下,所有这些选项都会起作用:select comment, COMMENT, "COMMENT" from ...
  • 但我认为他的问题是COMMENT这个词是oracle中的保留字。如果您创建一个包含名为COMMENT 的列的表,您仍然无法选择该列。此外,查看他在同一个表中使用的其他列,它们都不需要小写引号。
  • 啊!没明白重点,抱歉。当然,您不能将列名创建为 COMMENT,但“COMMENT”会起作用。应该打开我的大脑。或者去睡觉。
  • 事实上,我来自这里的 KOMENTAR,@thatjeffsmith :)
【解决方案2】:

感谢您的 cmets。好吧,现在我看到我在打字时必须更加小心。该列名为 comment(小写,不带引号)。

如果我运行以下简单查询,它会起作用: 从“time_recs”中选择“评论”;

但是,我无法使用缩写的表名。 – 我还提供了表格字段列表screenshot of table fields

【讨论】:

    猜你喜欢
    • 2010-11-12
    • 2012-02-03
    • 2021-05-15
    • 2013-12-14
    • 1970-01-01
    • 2014-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多