1

I have tried to write below SQL query in MS SQL server, but it is showing syntax error near WHERE.

Select ECUID,ECUNAME FROM (SELECT [ECU-ID] as ECUID, [ECU-NAME]as ECUNAME FROM TBL_ECU_MAST) WHERE ECUID = 147; 

Please help me to solve this.

1
  • 2
    All subqueries require an alias in SQL Server. I view this as a typographical error. Commented Apr 26, 2017 at 11:58

4 Answers 4

3

Just give your expression an Alias;

Select aliasHere.ECUID, aliasHere.ECUNAME FROM (SELECT [ECU-ID] as ECUID, [ECU-NAME]as ECUNAME FROM TBL_ECU_MAST) aliasHere WHERE aliasHere.ECUID = 147; 

Obviously use a more descriptive alias...

Sign up to request clarification or add additional context in comments.

Comments

3

you can use another way.like below code

;with t as ( SELECT [ECU-ID] AS ECUID, [ECU-NAME] AS ECUNAME FROM TBL_ECU_MAST ) SELECT ECUID,ECUNAME FROM t WHERE ECUID = 147 

Comments

2

As @Gordon suggested you have to give an alias name for the SubQuery used within the SQL Statement

SELECT ECUID,ECUNAME FROM (SELECT [ECU-ID] AS ECUID, [ECU-NAME] AS ECUNAME FROM TBL_ECU_MAST) T WHERE T.ECUID = 147; 

Hope this helps!!

Comments

0

Give alias name for all sub queries :

SELECT A.ECUID, A.ECUNAME FROM ( SELECT [ECU-ID] as ECUID, [ECU-NAME]as ECUNAME FROM TBL_ECU_MAST ) A WHERE A.ECUID = 147; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.