Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am using sql server 2012. Can anybody please guide how I can combine the following 2 commands into one.

SQL
Select distinct PROJN from PROJ
 JOIN [USER] ON PROJ.PROJWD=[USER].USERT WHERE ([USER].USERID=@USER)
 ;

Select distinct PROJN from PROJ
WHERE PROJ.PROJWD IN (SELECT DEPTN FROM DEPT WHERE DEPTH=@USER or deptd=@USER)


I require the resultant datatable to have distinct combine result of both queries.
For coding I am using vb in asp.net.

Thanks in advance..
Posted
Comments
PIEBALDconsult 23-Nov-14 0:45am    
UNION ?
atul sharma 5126 23-Nov-14 1:24am    
Thanks! If this is put in solution I will accept the same
Jörgen Andersson 23-Nov-14 4:22am    
How do you want the results combined?
Do you want the rows that satisfies both queries, or the rows that satisfies either query

You can try this also
SQL
Select distinct PROJN from PROJ
 JOIN [USER] ON PROJ.PROJWD=[USER].USERT WHERE ([USER].USERID=@USER)
or PROJ.PROJWD IN (SELECT DEPTN FROM DEPT WHERE DEPTH=@USER or deptd=@USER)
 
Share this answer
 
SQL
You can use either union or union all to combine the two queries.


Select distinct PROJN from PROJ
 JOIN [USER] ON PROJ.PROJWD=[USER].USERT WHERE ([USER].USERID=@USER)

union/Union all

Select distinct PROJN from PROJ
WHERE PROJ.PROJWD IN (SELECT DEPTN FROM DEPT WHERE DEPTH=@USER or deptd=@USER)
 
Share this answer
 
Use of UNION has solved the matter as suggested by PIEBALDconsult.

Thanks
 
Share this answer
 

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