Rajesh123
Rajesh123

Reputation: 107

"Incorrect syntax near the keyword 'WHERE'" in SQL Server in from clause

SELECT 
   [EmpNum], [EmpEmergencyContact], [Relation], [PhType], [Phone] 
FROM 
    (SELECT 
        [EmpNum], [EmpEmergencyContact], [Relation], [PhType], [Phone] 
    FROM [EMERGENCY_CONTACT])

A simple query where I have a SELECT statement within FROM clause returns an error:

Incorrect syntax near ')'

Upvotes: 3

Views: 7849

Answers (1)

Arion
Arion

Reputation: 31249

You need to put an alias on the from statement.

So change this:

   FROM [EMERGENCY_CONTACT]
)

To this:

   FROM [EMERGENCY_CONTACT]
) AS tbl

Like this:

SELECT [EmpNum], [EmpEmergencyContact], [Relation], [PhType], [Phone] 
FROM (
    SELECT [EmpNum], [EmpEmergencyContact], [Relation], [PhType], [Phone] 
    FROM [EMERGENCY_CONTACT]
) AS tbl

Even safer would be to use the alias on the columns like this:

SELECT 
    tbl.[EmpNum], 
    tbl.[EmpEmergencyContact], 
    tbl.[Relation], 
    tbl.[PhType], 
    tbl.[Phone] 
FROM (
    SELECT [EmpNum], [EmpEmergencyContact], [Relation], [PhType], [Phone] 
    FROM [EMERGENCY_CONTACT]
) AS tbl

Upvotes: 13

Related Questions