 |
| SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

October 30th, 2008, 06:00 AM
|
|
Registered User
|
|
Join Date: Oct 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Union of Two data Sets
Hi all,
I want to take the union of two data sets. Both data set are the result of different query. So i cant't use the below syntax.
Select * from Sample where id = 2
Union
Select * from Sample where id = 3
Please provide me the alternate solution. One data set i am getting as the result of inflectional search and one is the result of Stored procedure.
Thanks in advance
Tushar
|
|

October 30th, 2008, 03:24 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
Well, you really would be better off doing the UNION in SQL, despite what you say. It would be *MUCH* more efficient. There's no reason you couldn't do a union based on those two results.
But anyway...
And you aren't really talking about union of two DataSet objects, I hope. I hope/assume you are talking about the union of two DataTable objects.
So it's really easy enough.
Just use AddRow method on the destination DataTable to add a row you get form the source DataTable.
You don't say what language you are using, so I'm not going to bother with a code example.
|
|

October 30th, 2008, 11:37 PM
|
|
Registered User
|
|
Join Date: Oct 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi,
Thanks for your response.
I am using C#. As i said i want to take the union of two data sets in such a way that duplicate data should not be repeated.
Kindly reply me with code example.
|
|

October 31st, 2008, 02:39 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
One more time: NO, you are NOT taking the union of two data *SETS*. You are getting the union of two data *TABLES*. It's nonsense to talk about the union of data *SETS*.
Read what I wrote.
Code example tomorrow. Too late tonight.
|
|

October 31st, 2008, 03:24 AM
|
|
Registered User
|
|
Join Date: Oct 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Ya, I forgot to mention for that. I am taking the union of two data tables only. Waiting for the code example.
|
|

October 31st, 2008, 04:32 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
Okay, *IF* the two DataTable objects are completely compatible--that is, same number and *TYPES* of columns, it's especially easy:
Code:
DataTable t1 = ...wherever you get this from...;
DataTable t2 = ...wherever you get this from...;
foreach(DataRow r2 in t2.Rows)
{
t1.Rows.Add(r2);
}
If the schemas aren't the same, you'll have to create the new rows from the old ones on a field by field basis. Example:
Code:
DataTable t1 = ...wherever you get this from...;
DataTable t2 = ...wherever you get this from...;
foreach(DataRow r2 in t2.Rows)
{
DataRow r1 = t1.NewRow();
r1["name"] = r2["username"];
r1["addr"] = r2["homeAddress"];
r1["ssn"] = r2["socialSecurityNumber"];
rs["annualSalary"] = (double)(r2["intMonthlyWage"] * 12);
t1.Rows.Add(r1);
}
Notice that you can alter the values and/or change types via cast as you do the copy if needed.
|
|

November 4th, 2008, 07:55 AM
|
|
Registered User
|
|
Join Date: Oct 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi,
Thanks a lot for your response:). Now as per requirement i have to take the union of two list in such a way that there should not be any duplicates objects in the list... I have one property of objects so that i can compare the objects based on that property Say object.ID
Data type of List is Classobject.
Thanks in advance...
Tushar
|
|

November 4th, 2008, 05:23 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
Sorry, but now I am *SURE* you are making a big mistake.
Removing duplicates is *CLEARLY* a job for SQL, *NOT* for a DataTable or any other ADO.NET control.
Unless you have only a few records (less than one or two hundred) to be added from one table to the other, I can't possibly recommend doing this in .NET coding.
Yes, you *CAN* do this. You can filter the rows in DataTable1 based on the ID value from DataTable2. If you get no records from the filter, then you can add the record from 2 to 1. If you get one or more records, the ID is a duplicate, so it isn't added.
But this *will* be somewhat slow and clumsy compared to doing in SQL.
|
|
 |