What's wrong with my sql in oracle? - sql

What's wrong with my sql in oracle? There are some data in my table,I select all of them and I can get them.but I can not search them if I add a condition.When did I add single or double quotation marks in my sql?Now I find that when I write some search statement,I must add single quotation marks.And when I write some insert statement,I must add double quotation marks.Or my sql will run bad.How to judge when should I use the different quotation in my sql?
select * from T_STUDENT
and the result is:
sex(varchar2) phone(varchar2) birthtime(timestamp)
1 13553812147 2016-06-03 16:02:00.799 **
When I add a search condition,but the result is null.
//error:ORA-000904
select * from T_STUDENT where phone='13553812147'
//error:ORA-000904
select * from T_STUDENT where PHONE='13553812147'
//run well but result is null
select * from T_STUDENT where 'phone'='13553812147'
And the same question I meet in the insert statement.
//error:ORA-000904
insert into T_STUDENT (sex,phone,birthtime) values('1','12345645454','2016-06-04 16:02:00.799')
//error:ORA-000928 missing select keyword
insert into T_STUDENT ('sex','phone','birthtime') values('1','12345645454','2016-06-04 16:02:00.799')
//run well but must add double quotation marks
insert into T_STUDENT ("sex","phone","birthtime") values('1','12345645454','2016-06-04 16:02:00.799')

This is because your table was defined using double quotes around the column names:
create table t_student
( "sex" varchar2(1)
, "phone" varchar2(30)
, "birthtime" timestamp
);
Using double quotes makes the names case-sensitive and so for ever after they must be referenced in double quotes, since by default Oracle is nicely case-insensitive. For this reason you should never use double quotes when creating tables, views etc.

I've had the chance to look at this and Tony Andrews' answer is correct, you actually get invalid identifier as error message when you type an invalid column, which includes a case mismatch, though the error message will mention the exact identifier:
SQL> select * from T_STUDENT where phone='13553812147';
select * from T_STUDENT where phone='13553812147'
*
ERROR at line 1:
ORA-00904: "PHONE": invalid identifier
SQL> select * from T_STUDENT where funny_bunny='13553812147';
select * from T_STUDENT where funny_bunny='13553812147'
*
ERROR at line 1:
ORA-00904: "FUNNY_BUNNY": invalid identifier
The only thing missing from his answer is that Oracle will always make an internal cast to uppercase for any unquoted identifier, as the full error message illustrates. That's why phone='13553812147' won't match a column defined as "phone" (but "phone"='13553812147' will do).
Last but not least, single quotes define plain strings rather than object names so when you do this:
select * from T_STUDENT where 'phone'='13553812147'
... you aren't filtering by phone column at all. Instead, you have a constant condition that's always false (text "phone" equals text "13553812147").

Related

Snowflake column with triple double-quotes is always flagged as "Invalid identifier"

Multiple snowflake columns are like this """SOME TEXT WITH SPACES"""
No issue when I'm doing a SELECT * FROM MY_TABLE
But I can't figure out how to query the columns with double-quotes...
If I do :
SELECT """SOME TEXT WITH SPACES""" FROM MY_TABLE
I receive
SQL compilation error: error line 1 at position 7 invalid identifier '""SOME TEXT WITH SPACES""'
I've tried adding other double-quotes but I can't find the right combination...
If you have a table when selected from:
select * from names;
looks likes this:
NO_QUOTE
also_no_quote
"single_quotes"
"""triple_quotes"""
0
0
1
3
You need to understand how it was created, which is also how it need to be accessed :
There is the first layer of double quotes to turn off the case insensitivity.
Then for each extra layer of wanted double quotes in the output you have to use two double quotes on both sides.
Thus tripple quotes was made and accessed by 7 double quotes:
create or replace table names (no_quote int,
"also_no_quote" int,
"""single_quotes""" int,
"""""""triple_quotes""""""" int);
insert into names values (0,0,1,3);
and thus can be accessed by:
select no_quote, "also_no_quote", """single_quotes""", """""""triple_quotes""""""" from names;
You need to select the column name using quotes because column names are case sensitive if they are created in double quotes.
Example
create or replace table doublequotes (
seq int,
"""last_name""" string,
first_name string
);
insert into doublequotes values (10, 'abcd', 'efgh');
select """last_name""" from doublequotes;
o/p: abcd
Please try.

How to SELECT COL_"NAME" in Snowflake ? (COLUMN name containing double quote)

I know this is very bad naming practice, but I am not the owner of the table...
I need to run :
SELECT COL_"NAME"
FROM TABLE
where COL_"NAME" is the name of the column, containing double quotes.
I tried :
SELECT COL_""NAME""
FROM TABLE
SELECT COL_\"NAME\"
FROM TABLE
But nothing works
Identifier Requirements:
To use the double quote character inside a quoted identifier, use two quotes.
To access column: COL_"NAME"
SELECT "COL_""NAME"""
FROM TABLE;

Column isn't being found?

I am trying to display the username, last name, join date and country name for all the users who joined after the 13th January 2017 in ascending order. However each time I try to call any column, I get the error message "Column: Invalid Identifier".
SELECT Username, LastName, JoinDate, CountryName
FROM BR_USER, BR_COUNTRY
WHERE JoinDate = '01-JAN-17' AND JoinDate IS NOT NULL
ORDER BY JoinDate ASC;
Here is an image of how the BR_USER table is created.
Simple codes like:
SELECT UserId
FROM BR_USER;
Gives the same invalid identifier error, help
As the documentation explains:
Every database object has a name. In a SQL statement, you represent the name of an object with a quoted identifier or a nonquoted identifier.
A quoted identifier begins and ends with double quotation marks ("). If you name a schema object using a quoted identifier, then you must use the double quotation marks whenever you refer to that object.
If you define a column name with double quotes, you are condemned to using the double quotes whenever your reference that column. Or table or anything else with a name.
Actually, I don't think the documentation is 100% correct. Oracle uppercases all identifiers for resolution. So, if you define a quoted identifier with all upper case, then it will work without quotes. So this works:
create table t (
"COL" int
);
select "COL", COL, col
from t;
Here is a db<>fiddle.
But who wants to remember such rules -- rules so arcane and complex that the documentation is even misleading.
Simple solution: Don't use double quotes.

Oracle - Invalid Identifier for column name

I ran into an interesting situation with a NUMBER field.
Here is a simplified example.
When I select a particular field have happens to be 29 characters long, it works just fine.
select FIELD_NAME_THAT_IS_29_CHAR_XX
from table;
In the report where this query is being used, the query does not return headers (this is an eText type XML publisher report). But when I run the query with a UNION, selecting the header names, I get an invalid identifier error.
SELECT "FIELD_NAME_THAT_IS_29_CHAR_XX" FROM dual
UNION
SELECT FIELD_NAME_THAT_IS_29_CHAR_XX FROM table1;
Returns:
ORA-00904: "LINE_RECEIPT_AMNT_AT_COST_USD": invalid identifier
The max length of a field name in Oracle DB is 30 chars. Am I hitting this limitation? I think not as this, for example:
SELECT "FIELD_NAME_THAT_IS_29_CHAR_XXxxxxx" FROM dual;
..gives:
ORA-00972: identifier is too long
What is wrong with the UNION?
Single quotes do not work either. This is a NUMBER field.
SELECT 'FIELD_NAME_THAT_IS_29_CHAR_XX' FROM dual
UNION
SELECT FIELD_NAME_THAT_IS_29_CHAR_XX FROM table1;
..gives:
ORA-01790: expression must have same datatype as corresponding expression
The problem is with the double quotes
Oracle SQL allows us to ignore the case of database object names provided we either create them with names all in upper case, or without using double quotes. If we use mixed case or lower case in the script and wrapped the identifiers in double quotes we are condemned to using double quotes and the precise case whenever we refer to the object or its attributes:
Instead use single quotes which essentially works and will give you the result you want
http://sqlfiddle.com/#!4/dce84/4
#Standin answer lead me to solution.
Because the selected field is a number, just need to cast is as a char - along with the single quotes.
SELECT 'FIELD_NAME_THAT_IS_29_CHAR_XX' FROM dual
UNION
SELECT to_char(FIELD_NAME_THAT_IS_29_CHAR_XX) FROM table1;

A CREATE statement with quoted fields in Oracle

I have created a table in Oracle 10g using the following CREATE statement.
CREATE TABLE test ("id" NUMBER(35, 0) primary key, "description" VARCHAR2(250) not null);
The basic table structure looks like as follows.
--------------------------------------------------------------------------------
Column Name Data Type Nullable Default Primary Key
--------------------------------------------------------------------------------
id NUMBER(35, 0) No - 1
description VARCHAR2(250) No - -
It should precisely be noted that the column names in this CREATE statement are enclosed within double quotes just for having a fun :)
After issuing this DDL statement, I issued three DML statements to add this many rows as follows.
INSERT INTO test VALUES (1, 'aaa');
INSERT INTO test VALUES (2, 'bbb');
INSERT INTO test VALUES (3, 'ccc');
And finally, the following SELECT statement was executed to verify, if those rows were inserted.
SELECT * FROM test;
Oracle indeed displays three rows exactly as inserted on executing this query.
But when I issue the following SELECT query,
SELECT id, description FROM test;
Oracle complains,
ORA-00904: "DESCRIPTION": invalid identifier
The following (same) query also,
SELECT id FROM test;
fails with the error,
ORA-00904: "ID": invalid identifier
The same is true for the query,
SELECT description FROM test;
The only SELECT query with the meta character * works. Listing fields in the SELECT clause doesn't work. Capitalizing the column names in the SELECT clause also doesn't work.
What is the reason behind it?
Please don't just say, Don't do this. I'm interested in knowing the reason behind it.
OK, I won't say it, I'll just think it loudly.
The documentation clearly says that if you have quoted identifiers, you have to quote them everywhere (my italics for emphasis):
Every database object has a name. In a SQL statement, you represent the name of an object with a quoted identifier or a nonquoted identifier.
A quoted identifier begins and ends with double quotation marks ("). If you name a schema object using a quoted identifier, then you must use the double quotation marks whenever you refer to that object.
A nonquoted identifier is not surrounded by any punctuation.
So you always have to do:
SELECT "id", "description" FROM test;
Which is a pain. But obviously I'm just thinking that too, not really saying it.