0

I have a database called Inspection, and a table called User.

I first try to query it as follows:

select * from User ;

... and then like this:

select * from Inspection.dbo.User ;

Both of these are throwing the following error:

Incorrect syntax near the keyword 'User'

Why am I getting this error?

Kjartan
  • 18,591
  • 15
  • 71
  • 96
tester Joe
  • 73
  • 2
  • 5
  • 15
  • i solve it i have to write it like "select * from [Inspection].[dbo].[User]" when i am giving it data base name in connection string then why it is acting like this ? – tester Joe Apr 29 '13 at 07:32
  • I see you found a solution already. My answer provides an explanation for it though, see below! :) – Kjartan Apr 29 '13 at 08:14

1 Answers1

0

You need to use square brackets around the name of the table, since you use the name user.

Since user is a reserved word (has a special meaning on it's own), it can not be used directly as a table-name.

The square brackets tells MS Sql that in this case, [user] is the name of something a user (you!) have defined (namely a table), and not a keyword in an operation on an actual user, as in for example:

CREATE USER slartibartfast (...);

Update: More info about this here.

Community
  • 1
  • 1
Kjartan
  • 18,591
  • 15
  • 71
  • 96