使用缩写表名在Oracle中转义保留字的语法

时间:2019-04-03 19:31:16

标签: oracle oracle-sqldeveloper reserved-words

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

例如:如果我运行,请从“ 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
  ;

2 个答案:

答案 0 :(得分:3)

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

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>

答案 1 :(得分:0)

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

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

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

的列表
相关问题