SQL Server - Incorrect syntax near)

Can you view this code? I got this error, and I'm not sure what causes it:

Incorrect syntax next to ')'.

select * from (select distinct sar90.code, sar90.state, sar90.county, sabet.code, sabet.state, sabet.county from [dbo].[sarshomari_90] as sar90, [dbo].[Fixed] as sabet where sar90.county = sabet.county and sar90.state = sabet.state and sar90.state = N'kerman') 
+5
source share
2 answers

You need the alias of your subquery. However, you do not need to use a subquery for this. You can use SELECT DISTINCT directly. Also, avoid using old-style JOIN syntax and use an explicit JOIN expression instead.

However, if you want to use a subquery, your column must have unique names. Do this by adding unique aliases.

 select * from( select distinct sar90.code as code1, sar90.state as state1, sar90.county as country1, sabet.code as code2, sabet.state as state2, sabet.county as country2 from [dbo].[sarshomari_90] as sar90 inner join [dbo].[Fixed] as sabet on sar90.county = sabet.county and sar90.state = sabet.state where sar90.state = N'kerman' )t 
+7
source

Add an alias to the subquery and an alias to the column to avoid ambiguous names.

 select * from (select distinct [code1] = sar90.code, [state1] = sar90.state, [county1] = sar90.county, [code2] = sabet.code, [state2] = sabet.state, [county2] = sabet.county from [dbo].[sarshomari_90] as sar90, [dbo].[Fixed] as sabet where sar90.county=sabet.county and sar90.state= sabet.state and sar90.state=N'kerman') AS tab 
+3
source

All Articles