Can anyone help with solving following Oracle SQL Errors: (E26_0002) & (ORA-00936)? Below is my script
SELECT Number, ID, NAME, JUDGE, MODIFY_DATE,
FROM Mine
WHERE
Number IN (‘221130G32’)
Thanks!
I added commas where they were missing and it still didn't help.
Remove a comma in front of the from keyword (but that's a minor mistake). There seems to be a worse one.
Column name can't be Number; that's a reserved word (for datatype).
SQL> create table mine (number varchar2(10));
create table mine (number varchar2(10))
*
ERROR at line 1:
ORA-00904: : invalid identifier
It can be, but only if you enclose its name into double quotes, but then you have to reference that column using double quotes and exactly the same letter case every time (and that would be really bad idea):
SQL> create table mine ("Number" varchar2(10));
Table created.
As condition you used clearly says that column's contents is a string, then rename the column and use appropriate datatype.
Anyway, presuming that column name really is Number, then you'd
SQL> create table mine as
2 select '221130G32' as "Number", 1 id, 'Little' name, 'Foot' judge, sysdate modify_date from dual;
Table created.
SQL> select "Number", id, name, judge, modify_date
2 from mine
3 where "Number" in ('221130G32');
Number ID NAME JUDG MODIFY_DA
--------- ---------- ------ ---- ---------
221130G32 1 Little Foot 16-DEC-22
SQL>
Related
Based on past experience and documentation related to search_path:
"The first matching table in the search path is taken to be the one wanted. If there is no match in the search path, an error is reported, even if matching table names exist in other schemas in the database."
But when I run this:
financialdw=> show search_path;
search_path
------------------------------------------------------
"$user, prismdms, prism_nonrestrict, prism_restrict"
(1 row)
financialdw=> select count(*) from addr;
ERROR: relation "addr" does not exist
LINE 1: select count(*) from addr;
^
financialdw=> select count(*) from prismdms.addr;
count
-------
24428
(1 row)
prismdms is in my search_path and I have the necessary permissions on all tables within it. Shouldn't I be able, as the document states, query the table without the schema name qualifying it?
Your search_path has a single schema in it, and that single schema has the long and unlikely name of $user, prismdms, prism_nonrestrict, prism_restrict.
The double quotes should just be around $user, not the entire thing.
I encountered a strange issue this morning. I was creating a view simplifying a list of applications in a Postgres table.
This failed.
CREATE OR REPLACE VIEW application_view AS
SELECT COALESCE( nullif(full_name,''), nullif(additional_info,''), app_name) name
, id
FROM application
ORDER BY COALESCE( nullif(full_name,''), nullif(additional_info,''), app_name)
whereas
CREATE OR REPLACE VIEW application_view AS
SELECT COALESCE( nullif(full_name,''), nullif(additional_info,''), app_name) application
, id
FROM application
ORDER BY COALESCE( nullif(full_name,''), nullif(additional_info,''), app_name)
worked.
I often use name as a column name in tables so any ideas as to why the first sql statement failed?
It's a keyword. When you want to use a keyword as an alias in the select list you have to use the word as:
select 1 name;
ERROR: syntax error at or near "name"
LINE 1: select 1 name;
select 1 as name;
name
------
1
(1 row)
From the documentation about aliases in the select list:
To specify the name to use for an output column, write AS output_name after the column's expression. (You can omit AS, but only if the desired output name does not match any PostgreSQL keyword (see Appendix C). For protection against possible future keyword additions, it is recommended that you always either write AS or double-quote the output name.)
So I am reading tutorial online and I am having a weird issue with my database when doing normal queries. So I see the following does not work:
select * from DBS
ERROR: relation "dbs" does not exist
but This works:
select * from "DBS"
When I do this it fails:
select name from "DBS"
ERROR: column "name" does not exist
but this works but doesnt actually return the correct information (it just has name for every row:
select 'name' from "DBS"
name
name
name
Is there some setting on Postgres causing this to happen?
Postgres 9.4.5 (On RDS).
select 'NAME' from "DBS";
?column?
----------
NAME
NAME
NAME
(3 rows)
When I look at select * from "DBS";
NAME
----------
default
matt
matt2
You need to specify the quotes around the table identifier, because the table uses capital letters and was created using quotes.
postgres has some distinctive behavior re: quoting
select 'name' from "DBS"
You are simply selecting the string literal 'name' once for each row in your table.
Postgres is a little unique in that it folds everything to lowercase unless you use " quotes. I suspect you created your database using pgadmin. If you create a database in pgadmin using UPPER case it will create it with UPPER case and then you will be required to use " around the name to access it. Candidly I'd rename the db to lower case and get rid of the quotes, your life will be a lot easier.
I am trying to create a summary by country for all the active fires and group them according to month
SELECT
date_part('month',acqdate) AS month
,date_part('year',acqdate) AS year
, count(the_geom) as num_fires
FROM
merged_activefires
JOIN
"california_county.wgs84"
ON
ST_Intersects(merged_activefires.the_geom, california_county.wgs84.the_geom)
WHERE
california_county.wgs84.name = 'Modoc' --acqdate >'2013-12-31' and acqdate<'2014-02-01'
and
GROUP BY date_part('month',acqdate),date_part('year',acqdate)
ORDER BY month;
ERROR: missing FROM-clause entry for table "wgs84"
LINE 4: ON ST_Intersects(merged_activefires.the_geom, california_cou...
^
****** Error ******
ERROR: missing FROM-clause entry for table "wgs84"
SQL state: 42P01
Character: 209
In the JOIN clause, this syntax with double quotes around the entire name:
"california_county.wgs84"
means that this entire name designates a table, as opposed to a schema name followed by a dot followed by a table name, which could be written like this: california_county.wgs84 or like that: "california_county"."wgs84"
On the other hand, this other part of your query:
california_county.wgs84.name
suggests exactly the opposite, that california_county is a schema name and wgs84 is a table inside it, and name is a column of that table.
It is this contradiction that confuses the SQL parser.
Either "california_county.wgs84" is a table name in the current schema (an unfortunate choice, but valid) and you should consistently refer to it with "california_county.wgs84",
or california_county is a schema and you should just use california_county.wgs84 without any quote.
I am using Ubuntu and PostgreSql 8.4.9.
Now, for any table in my database, if I do select table_name.name from table_name, it shows a result of concatenated columns for each row, although I don't have any name column in the table. For the tables which have name column, no issue. Any idea why?
My results are like this:
select taggings.name from taggings limit 3;
---------------------------------------------------------------
(1,4,84,,,PlantCategory,soil_pref_tags,"2010-03-18 00:37:55")
(2,5,84,,,PlantCategory,soil_pref_tags,"2010-03-18 00:37:55")
(3,6,84,,,PlantCategory,soil_pref_tags,"2010-03-18 00:37:55")
(3 rows)
select name from taggings limit 3;
ERROR: column "name" does not exist
LINE 1: select name from taggings limit 3;
This is a known confusing "feature" with a bit of history. Specifically, you could refer to tuples from the table as a whole with the table name, and then appending .name would invoke the name function on them (i.e. it would be interpreted as select name(t) from t).
At some point in the PostgreSQL 9 development, Istr this was cleaned up a bit. You can still do select t from t explicitly to get the rows-as-tuples effect, but you can't apply a function in the same way. So on PostgreSQL 8.4.9, this:
create table t(id serial primary key, value text not null);
insert into t(value) values('foo');
select t.name from t;
produces the bizarre:
name
---------
(1,foo)
(1 row)
but on 9.1.1 produces:
ERROR: column t.name does not exist
LINE 1: select t.name from t;
^
as you would expect.
So, to specifically answer your question: name is a standard type in PostgreSQL (used in the catalogue for table names etc) and also some standard functions to convert things to the name type. It's not actually reserved, just the objects that exist called that, plus some historical strange syntax, made things confusing; and this has been fixed by the developers in recent versions.
According to the PostgreSQL documentation, name is a "non-reserved" keyword in PostgreSQL, SQL:2003, SQL:1999, or SQL-92.
SQL distinguishes between reserved and non-reserved key words. According to the standard, reserved key words are the only real key words; they are never allowed as identifiers. Non-reserved key words only have a special meaning in particular contexts and can be used as identifiers in other contexts. Most non-reserved key words are actually the names of built-in tables and functions specified by SQL. The concept of non-reserved key words essentially only exists to declare that some predefined meaning is attached to a word in some contexts.
The suggested fix when using keywords is:
As a general rule, if you get spurious parser errors for commands that contain any of the listed key words as an identifier you should try to quote the identifier to see if the problem goes away.