I'm using this piece of code to get the number of search results. However it says- Incorrect syntax near ')' (the very last ')' )

SELECT COUNT(*) FROM
(SELECT  ID, ProductDescription FROM Products WHERE ProductDescription LIKE '%tech%')

The inner select works fine and gives correct results, so I don't understand what's the problem... I won't be surprised if it's something stupid, but I've tried lots of things but with no success..
So I hope you guys can give some hints.

Recommended Answers

You need to name the table alias in your sub select.

This will fail:
[code=sql]
Select Count()
From (Select
From sysobjects)
[/code]

This will work:
[code]
Select Count()
From (Select
From sysobjects) [COLOR="Red"]as tbl[/COLOR]
[/code]

Just stick an "as tblName" clause regardless of whether you use it.

In …

Jump to Post

All 2 Replies

You need to name the table alias in your sub select.

This will fail:

Select Count(*)
From (Select * From sysobjects)

This will work:

Select Count(*)
From (Select * From sysobjects) as tbl

Just stick an "as tblName" clause regardless of whether you use it.

In the future please post Microsoft SQL Questions to the MSSQL Forum.

Thank you very much!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.