Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

sql - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...