Ruben Gullini
Ruben Gullini

Reputation: 31

Syntax to escape a reserved word in Oracle while using abbreviated table names

I have an Oracle 11g database with table named "time_recs". I need to run a query in SQL Developer, including a column named comment. As comment is a reserved word, I need to escape it, that´s ok. The problem is I can´t find the correct syntax when you are using abbreviated table names.

E.g.: If I run select "comments" from "time_recs"; it works well.

But the report I need to run is pasted below. If you see line 17 ("tr"."comment"), I´ve tried a lot of different things: quotes, double quotes, etc. Still I can´t make it work as I keep getting the "invalid identifier" error

Any help will be really appreciated.

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
  ;

Upvotes: 2

Views: 2246

Answers (2)

Ruben Gullini
Ruben Gullini

Reputation: 31

thank you for your comments. Well, now I see that I have to be more careful with typing. The column is named comment (lowercase without quotes).

It works if I run the following simple query: select "comment" from "time_recs";

However, I can´t make it work with the abbreviated table names. – I´m also providing a list of the table fields screenshot of table fields

Upvotes: 1

Littlefoot
Littlefoot

Reputation: 142705

You don't need anything; use alias as is:

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>

Upvotes: 3

Related Questions