Click here to Skip to main content
15,901,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi .
why this query does not work ?
SQL
SELECT TOP 50 * FROM 
SELECT  fIDUser1 AS fIDUser FROM UserFeature.Relationship WHERE fIDUser2=5 AND fIDStatus=141
          UNION ALL
          SELECT fIDUser2 AS fIDUser FROM UserFeature.Relationship WHERE fIDUser1=5 AND fIDStatus=141)


when execute show error :

Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'FROM'.
Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'SELECT'.
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near ')'.
Posted
Updated 26-Apr-12 2:05am
v3

SQL
SELECT TOP 50 * FROM 
(SELECT  fIDUser1 AS fIDUser FROM UserFeature.Relationship WHERE fIDUser2=5 AND fIDStatus=141
          UNION ALL
          SELECT fIDUser2 AS fIDUser FROM UserFeature.Relationship WHERE fIDUser1=5 AND fIDStatus=141)


should do the trick :

1- The first FROM must be moved outside the parenthesis
2- UNION ALL SELECT SELECT was incorrect
 
Share this answer
 
Comments
Saeid.Babaei86 26-Apr-12 8:06am    
not work too !
SQL
SELECT TOP 50 * FROM (

SELECT  fIDUser1 AS fIDUser FROM UserFeature.Relationship WHERE fIDUser2=5 AND fIDStatus=141
 UNION ALL
SELECT fIDUser2 AS fIDUser FROM UserFeature.Relationship WHERE fIDUser1=5 AND fIDStatus=141
) as  Users
 
Share this answer
 
v4
Comments
Saeid.Babaei86 26-Apr-12 8:11am    
Oh thank you ! that is right
Kschuler 26-Apr-12 9:55am    
If this solution worked for you, it would be nice of you to mark it as the correct solution so that we can see that your question has been answered.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900