How would I fix these "ORA-00933: SQL command not properly ended" "ORA-00923: FROM keyword not found where expected" errors? - sql

This Statement:
SELECT id, units, cost FROM inventory_list WHERE cost <= 20;
Gives me:
ORA-00923: FROM keyword not found where expected
While this statement:
SELECT * FROM items WHERE ilt_id = 'il010230126' OR ilt_id = 'il010230128';
Gives me:
ORA-00933: SQL command not properly ended

Not sure about this and may be version dependent (below link is for oracle 10g... but you can see on this site
https://docs.oracle.com/cd/B19306_01/em.102/b40103/app_oracle_reserved_words.htm
That cost is an oracle reserved keyword, so it is not wise to use it as a column name.
If you have no control of the table I think you may be able to surround it in double quotes eg select "COST" to avoid oracle picking it up as a reserved word.
By default Oracle creates fields in uppercase so the field name will need to be in uppercase unless when the table was created it was forced into different case by surrounding it in Quotes.

Check that you don't have invisible characters in your file and that you're using the right encoding. I sometimes accidentally introduce them because I have a non english keyboard map and accidentally hit the wrong key combination.
Just type again one of your SQL statements and test them.

Related

visual studio 2012 query builder

Can anybody tell me what does the error mean? Whenever I open the query builder it will prompt with an error indicating that SQL syntax errors were encountered.
https://msdn.microsoft.com/en-us/library/ms189012.aspx
I looked at the following page in MSDN but I don't understand what it means...
For instance, what do these bullet points from the MSDN article mean?
The SQL statement is incomplete or contains one or more syntax errors.
The SQL statement is valid but is not supported in the graphical panes (for example, a Union query).
The SQL statement is valid but contains syntax specific to the data connection you are using.
USER (which you've apparently decided is an appropriate table name) is a SQL Server reserved word.
The best solution is to rename your table, so you don't have to escape the table name every time you want to query it and to make it clear it's your user data (hey, there's a table name suggestion - userdata).
The other option is to escape the name by surrounding it with square brackets:
SELECT * FROM [users]
Note that it will get old fast having to do this with every query. Again, the best solution would be to rename the table to something that isn't a reserved word.

SQL select from table with alias (Oracle9i)

According to Oracle documentation for statement SELECT it should be possible to alias table names with aliases without or with keyword AS. However, aliasing tables with keyword AS leads to error:
ORA-00933: SQL command not properly ended
For example, the following statement fails with the above error:
SELECT COUNT(*) FROM MY_TABLE AS A;
Once keyword AS is removed it executes as expected.
Could anyone please comment on this. Is there a way to make the application of AS for table aliasing work?
P.S. I'm using a code generation utility that translate some Java code into SQL statements at runtime. This utility enforces the use of aliases with AS.
Oracle does not accept AS for table aliases and I see no way to make it work.
Can't you do anything in Java? AS for column aliases is optional in Oracle, so you could look for all " AS " in the generated string and remove them (thus removing AS for column aliases as well as for table aliases). Is this an option?

Simple SELECT statement fails with "syntax to use near", "ORA-00906", "syntax error at or near" or "syntax near the keyword"

I have a very simple SQL statement
SELECT * FROM Table;
but, my query engine returns a syntax error. Why?
Error Details:
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in >System.Data.dll
Additional information: Incorrect syntax near the keyword 'Table'.
How is this possible? I checked the connection string and it is correct.
I checked my table name and it is also correct.
What I am doing wrong?
Okay, Table is a reserved keyword in all variants of SQL.
If you want to call a table Table, and use it in a statement, you have to tell your sql engine that it is an identifier. To do this you need to use Identifier Qualifiers.
for (MS SQL Server) TSQL use square brackets
SELECT * FROM [Table];
for MySQL use `
SELECT * FROM `Table`;
for Oracle and PostgreSQL use quotation marks,
these are standards compliant.
SELECT * FROM "Table";
for SQLite you can use any of the above, but quotation marks are prefered.
The Identifier Qualifiers tell the engine that this is an identifier (the name of an object.) Not the name of a keyword, even if they happen to be the same. Without your guidance the query engine can get confused and report an error, or worse, do something unexpected.
Using Identifier Qualifiers is good practice, even if the identifers are not keywords.
They better define statements for all parsers, including the fleshy kind.
Naming objects after keywords is generally considered bad practice. So you should try to avoid making identifers the same as keywords. The occasions when a reserved keyword is descriptive of the contents of a table are rare, see the footnote.
e.g. your table is not a Table of tables.
The problem and advice is not limited to Tables, Identifiers are required for all database objects inluding Schema, Views and the many types that exist, standard and vendor-specific.
Another form of good practice is to prefix Table indentifiers with a Schema identifier, this helps the query engine a little.
When including the Schema identifier, the identifer should be qualified,
for (MS SQL Server) TSQL use square brackets
SELECT * FROM [dbo].[Table];
for MySQL use `
SELECT * FROM `dbo`.`Table`;
for Oracle, PostgreSQL and SQLite use quotation marks
SELECT * FROM "dbo"."Table";
even if your Schema is not named after a keyword, as should be the case.
For your reference, to help you avoid conflicts.
A list of TSQL Reserverd Keywords.
A list of MySQl Reserved Keywords.
A list of Oracle Reserved Keywords.
A list of SQLite Reserved Keywords.
A list of PostgreSQL Reserved Keywords.
Notable "gotcha's" include USER and ERROR, which seem to come up when designing systems.
Footnote:
There are occasions when using reseved words for object names may be semantically correct.
Consider the contrived example of an information system for a furniture shop. In this scenario, a table of tables (kitchen, garden, dining, apothecary etc.) may be correct. So, you could argue Table was the correct identifier.
If you always use Identifier Qualifiers, you won't get burned.
If you are using SQL server you need to wrap table in brackets [] as table is keyword in SQL Server
SELECT * FROM [Table]

Oracle: Handling a field named COMMENT

I have a table with a field named COMMENT, which appears to be a reserved word.
Using SQLDeveloper, if I try:
select
[COMMENT],
another_field
FROM table_created_by_idiot_developer
I get
SQL Error: ORA-00936: missing expression
How can I access this field in my select in SQL Developer? (Is this a problem with SQL Developer, or should field not be named COMMENT in oracle?)
Try "COMMENT" instead of [COMMENT]. This is alternate syntax commonly accepted by various DBMSes. I have used this syntax to refer to columns having dots or UTF8 characters in their names in SQLite.

copy one field from table to another field in the same table

I used this query to copy one full column from the same table:
UPDATE 'content_type_chapter'
SET 'field_chapternumbersort2_value' = 'field_chapternumbersort_value'
But I have received this error.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''content_type_chapter' SET 'field_chapternumbersort2_value'='field_chapternumber' at line 1
What could be wrong, I'm unable to get it right.
Single-quotes are for strings.
Try backticks instead, e.g.:
UPDATE
`content_type_chapter`
SET
`field_chapternumbersort2_value` = `field_chapternumbersort_value`
The backticks aren't strictly necessary, though.
Just leave the quotes off your field names, otherwise it thinks you are giving it strings