Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"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 ')'

like image 815
Rajesh123 Avatar asked Nov 30 '25 03:11

Rajesh123


1 Answers

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
like image 172
Arion Avatar answered Dec 02 '25 17:12

Arion



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!