-3

i have a table called ORDER, when i try to query it using

select * from Order

i get the error 'invalid query' how to do access this table please? thank you

  • In SQL Server, I would try "SELECT * FROM [Order]" – Tab Alleman Jan 13 '15 at 13:57
  • 2
    Please specify the RDBMS that you are targeting by adding the appropriate tag (Oracle, SQL Server, MySQL, etc.). There may be answers that take advantage of language or product features that are not universally supported. Also, by tagging it with a specific RDBMS, your question may receive attention from people better suited to answer it. – Taryn Jan 13 '15 at 13:58
  • 1
    In this particular case, the solution is specific to the database engine you're using. It would be `[ORDER]` in SQL Server and Access (and SQLite), `'ORDER'` in some other variants, and `\`ORDER\`` in still other variants. – Larry Lustig Jan 13 '15 at 14:00
  • @DavidFaber While that could answer the question, since they've failed to mention their database I wouldn't suggest a SQL Server solution as a duplicate. – Taryn Jan 13 '15 at 14:06
  • Closed as duplicate for a SQL-Server specific question. Not sure I agree with this one. Seems a bit contradictory to the comments by @bluefeet! – gvee Jan 13 '15 at 15:22

3 Answers3

5

Step 1

Don't ever used reserved words for object (or column) names.

Step 2

If you've inherited such a thing that you are not able to change, then you need to use "quoted identifiers".

I advocate

SELECT *
FROM   "Order"

As it is a standard identifier, so will work better across platforms.

Community
  • 1
  • 1
gvee
  • 16,732
  • 35
  • 50
  • Downvoted because? It would be nice to hear reasoning so answers can be adjusted. – gvee Jan 13 '15 at 14:00
  • I am guessing that someone thinks that double quotes are database-specific. You should probable edit to specify that they're standard in "recent" versions of SQL. – Larry Lustig Jan 13 '15 at 14:03
  • 1
    Recent? It's been there for ages, since SQL-1 or SQL-2? – jarlh Jan 13 '15 at 14:07
  • 1
    Yep, double quotes have been the standard for a long time. Source: SQL 1992 documentation - http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt – gvee Jan 13 '15 at 14:21
2

ORDER is a reserved word in SQL. Put double quotes around it:

select * from "Order"

And also, I personally think ORDERS is a better name. (Because several orders are stored in the table.)

Late edit: List of reserved words, in different versions of the SQL standard: http://developer.mimer.com/standard/reservedwords/sql-reserved-words.tml

jarlh
  • 42,561
  • 8
  • 45
  • 63
  • P.S. massive +1 for your recommended table name. It's very rare a table name shouldn't be a plural... how often does it only contain a single row! – gvee Jan 13 '15 at 15:19
-1

It's generally a good idea to not use reserved words for database object names (tables, views & etc.). Sometimes you just got to deal with it though. The below query should work for you.

select * from [Order]

Just put the brackets around the table name.

Hope this helps!

JaredW
  • 17
  • 4
  • 2
    What platform is OP using? – Kermit Jan 13 '15 at 14:23
  • I see where you are coming from. However, I can't post comments on questions since my rating is low since I'm a new member to the board. – JaredW Jan 13 '15 at 16:42
  • 1
    Until you have the necessary reputation, I would recommend that you wait for the OP to edit their question to contain everything you need to give a good answer. Otherwise, I would go to another question. – Kermit Jan 13 '15 at 17:51