Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Fetch similar columns from two tables

Author  Topic 

vignesht50
Yak Posting Veteran

82 Posts

Posted - 2013-12-10 : 05:27:12
Hi,

I am using this query to search checkbox values.


SqlCommand cmd = new SqlCommand("Select * from Details where Emp_Code in (" + selectedValues + ")", con);


I want to join a table called Materials to this now. Material table also has an Emp_Code column. How can I write a select sql query to fetch Emp_Code from both Details and Materials table.

SqlCommand cmd = new SqlCommand("select D.Emp_Code, M.Emp_Code from Details D, Materials M where D.Emp_Code = M.Emp_Code in (" + selectedValues + ")", con);

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-12-10 : 05:38:47
[code]
SqlCommand cmd = new SqlCommand("Select * from Details where Emp_Code in (" + selectedValues + ") union all Select * from Materials where Emp_Code in (" + selectedValues + ")", con);
[/code]
Assuming structure of both tables are same

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

vignesht50
Yak Posting Veteran

82 Posts

Posted - 2013-12-10 : 05:48:34
quote:
Originally posted by visakh16


SqlCommand cmd = new SqlCommand("Select * from Details where Emp_Code in (" + selectedValues + ") union all Select * from Materials where Emp_Code in (" + selectedValues + ")", con);

Assuming structure of both tables are same

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

https://www.facebook.com/VmBlogs




Hi Visakh,

I get this error.
All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.
Go to Top of Page
   

- Advertisement -