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

问题描述 投票:1回答:2

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

例如:如果我运行从“time_recs”中选择“评论”;它运作良好。

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

任何帮助将非常感激。

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
  ;
oracle oracle-sqldeveloper reserved-words
2个回答
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>

0
投票

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

如果我运行以下简单查询,它可以工作:从“time_recs”中选择“comment”;

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

© www.soinside.com 2019 - 2024. All rights reserved.