"Incorrect syntax near" WHERE "keyword in SQL Server from article

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

      

A simple query where I have a statement SELECT

in a section FROM

returns an error:

Incorrect syntax near ')'

+3


source to share


1 answer


You need to specify an alias in the from statement.

So, change this:

   FROM [EMERGENCY_CONTACT]
)

      

For 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

      

It would even be safer to use an alias on 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

      

+12


source







All Articles