Ananth
Ananth

Reputation: 10720

SQL Server Query with Derived Table Produces Syntax Error

Hi I have a situaltion similar to this

SELECT *
FROM
(
  SELECT *  
  FROM 
  Table1
)

I wonder why this gives an error

Incorrect syntax near ')'.

Any help ? Thanks in Advance..

Upvotes: 3

Views: 1178

Answers (3)

Mosty Mostacho
Mosty Mostacho

Reputation: 43464

Because you need to add an alias. Run it this way:

SELECT * FROM (
   SELECT * FROM Table1
) T

Just for the record, MySQL displays the following error given the same situation :)

Every derived table must have its own alias

Upvotes: 2

Richard
Richard

Reputation: 11

And you can use the AS keyword to make it more readable

SELECT * FROM ( SELECT * FROM table1 )  as table2

Upvotes: 1

usr
usr

Reputation: 171206

SELECT *
FROM
(
  SELECT *  
  FROM 
  Table1
) x

You need to give your derived table a name.

Upvotes: 6

Related Questions