473,385 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

compare 2 tables and store result in 3rd table??

VMI
I have two tables and I want to compare these two tables with a query( ie.
"select * from A where B.key = A.key") and the result will be stored in a
3rd table (table C). is this possible? If necessary, I can create the schema
for the 3rd table (C) since it'll be exactly like table A. But I'm more
interested in being able to store the resulting set into another table.

Thanks.
Nov 16 '05 #1
9 6591
VMI,

You can't really say:

select * from A where B.key = A.key

Because you don't know what the set of B is at that point. Are you
trying to check for the existence of the key in another table? If so, then
I would just do this:

select
a.*
from
a
inner join b on a.key = b.key

Since you use the field name "key", I'm assuming that each value is
unique, and therefore a one-to-one relationship exists.

If you perform this select, it will give you the same structure as A.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"VMI" <vo******@yahoo.com> wrote in message
news:eQ**************@TK2MSFTNGP14.phx.gbl...
I have two tables and I want to compare these two tables with a query( ie.
"select * from A where B.key = A.key") and the result will be stored in a
3rd table (table C). is this possible? If necessary, I can create the
schema for the 3rd table (C) since it'll be exactly like table A. But I'm
more interested in being able to store the resulting set into another
table.

Thanks.

Nov 16 '05 #2
> select
a.* into c from
a
inner join b on a.key = b.key

Nov 16 '05 #3
VMI
You're right about the query (it's been a long time since I've worked with
sql statements).
If I use this statement, where would I need to execute it in order to
compare datatable a and datatable b? Both tables are already filled with
data, and I need to store the data of the resulting set into a separate
table (which is a clone of a).

Thanks.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:ei**************@TK2MSFTNGP11.phx.gbl...
VMI,

You can't really say:

select * from A where B.key = A.key

Because you don't know what the set of B is at that point. Are you
trying to check for the existence of the key in another table? If so,
then I would just do this:

select
a.*
from
a
inner join b on a.key = b.key

Since you use the field name "key", I'm assuming that each value is
unique, and therefore a one-to-one relationship exists.

If you perform this select, it will give you the same structure as A.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"VMI" <vo******@yahoo.com> wrote in message
news:eQ**************@TK2MSFTNGP14.phx.gbl...
I have two tables and I want to compare these two tables with a query( ie.
"select * from A where B.key = A.key") and the result will be stored in a
3rd table (table C). is this possible? If necessary, I can create the
schema for the 3rd table (C) since it'll be exactly like table A. But I'm
more interested in being able to store the resulting set into another
table.

Thanks.


Nov 16 '05 #4
VMI,

That all depends on what kind of comparison are you doing? Are you
comparing A and B to see if the same keys exist in both tables? Or, where
the keys exist in both tables, the fields are the same?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"VMI" <vo******@yahoo.com> wrote in message
news:ev**************@TK2MSFTNGP10.phx.gbl...
You're right about the query (it's been a long time since I've worked with
sql statements).
If I use this statement, where would I need to execute it in order to
compare datatable a and datatable b? Both tables are already filled with
data, and I need to store the data of the resulting set into a separate
table (which is a clone of a).

Thanks.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:ei**************@TK2MSFTNGP11.phx.gbl...
VMI,

You can't really say:

select * from A where B.key = A.key

Because you don't know what the set of B is at that point. Are you
trying to check for the existence of the key in another table? If so,
then I would just do this:

select
a.*
from
a
inner join b on a.key = b.key

Since you use the field name "key", I'm assuming that each value is
unique, and therefore a one-to-one relationship exists.

If you perform this select, it will give you the same structure as A.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"VMI" <vo******@yahoo.com> wrote in message
news:eQ**************@TK2MSFTNGP14.phx.gbl...
I have two tables and I want to compare these two tables with a query(
ie. "select * from A where B.key = A.key") and the result will be stored
in a 3rd table (table C). is this possible? If necessary, I can create
the schema for the 3rd table (C) since it'll be exactly like table A. But
I'm more interested in being able to store the resulting set into another
table.

Thanks.



Nov 16 '05 #5
VMI
I'm just coparing the keys. For any matching keys between both tables (a &
b), I'll be copying the row from table a into my new table c. So I'll be
using the query you gave me, but I wouldn't know what other instructions I'd
execute it with.

Thanks.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eU****************@TK2MSFTNGP10.phx.gbl...
VMI,

That all depends on what kind of comparison are you doing? Are you
comparing A and B to see if the same keys exist in both tables? Or, where
the keys exist in both tables, the fields are the same?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"VMI" <vo******@yahoo.com> wrote in message
news:ev**************@TK2MSFTNGP10.phx.gbl...
You're right about the query (it's been a long time since I've worked
with sql statements).
If I use this statement, where would I need to execute it in order to
compare datatable a and datatable b? Both tables are already filled with
data, and I need to store the data of the resulting set into a separate
table (which is a clone of a).

Thanks.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:ei**************@TK2MSFTNGP11.phx.gbl...
VMI,

You can't really say:

select * from A where B.key = A.key

Because you don't know what the set of B is at that point. Are you
trying to check for the existence of the key in another table? If so,
then I would just do this:

select
a.*
from
a
inner join b on a.key = b.key

Since you use the field name "key", I'm assuming that each value is
unique, and therefore a one-to-one relationship exists.

If you perform this select, it will give you the same structure as A.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"VMI" <vo******@yahoo.com> wrote in message
news:eQ**************@TK2MSFTNGP14.phx.gbl...
I have two tables and I want to compare these two tables with a query(
ie. "select * from A where B.key = A.key") and the result will be stored
in a 3rd table (table C). is this possible? If necessary, I can create
the schema for the 3rd table (C) since it'll be exactly like table A.
But I'm more interested in being able to store the resulting set into
another table.

Thanks.



Nov 16 '05 #6
VMI
This is the code I was initially using:

DataTable table_auditGrid = _table_auditAddress.Clone();
foreach (DataRow row in table_keys.Rows)
{
DataRow[] resultRow = _table_auditAddress.Select("col_Key = '" +
row["key"] + "'");
if (resultRow.Length > 0)
{
table_auditGrid.Rows.Add(resultRow[0]); //error saying that this
row already exists in another table
}
}

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eU****************@TK2MSFTNGP10.phx.gbl...
VMI,

That all depends on what kind of comparison are you doing? Are you
comparing A and B to see if the same keys exist in both tables? Or, where
the keys exist in both tables, the fields are the same?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"VMI" <vo******@yahoo.com> wrote in message
news:ev**************@TK2MSFTNGP10.phx.gbl...
You're right about the query (it's been a long time since I've worked
with sql statements).
If I use this statement, where would I need to execute it in order to
compare datatable a and datatable b? Both tables are already filled with
data, and I need to store the data of the resulting set into a separate
table (which is a clone of a).

Thanks.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:ei**************@TK2MSFTNGP11.phx.gbl...
VMI,

You can't really say:

select * from A where B.key = A.key

Because you don't know what the set of B is at that point. Are you
trying to check for the existence of the key in another table? If so,
then I would just do this:

select
a.*
from
a
inner join b on a.key = b.key

Since you use the field name "key", I'm assuming that each value is
unique, and therefore a one-to-one relationship exists.

If you perform this select, it will give you the same structure as A.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"VMI" <vo******@yahoo.com> wrote in message
news:eQ**************@TK2MSFTNGP14.phx.gbl...
I have two tables and I want to compare these two tables with a query(
ie. "select * from A where B.key = A.key") and the result will be stored
in a 3rd table (table C). is this possible? If necessary, I can create
the schema for the 3rd table (C) since it'll be exactly like table A.
But I'm more interested in being able to store the resulting set into
another table.

Thanks.



Nov 16 '05 #7
VMI,

Ahh, I see what you are doing now. I would actually query against the
database itself, as the query would be much, much quicker to execute.

However, for what you are doing, that is the right way, with one
exception. Instead of calling the Add method on the DataRowsCollection, you
should call the ImportRow method. This will copy the row into the new
table.

Also, to make it easier (and not have to rely on the Select method), you
might want to establish a DataRelation between the two tables (assuming they
are in the same data set). Once you do that, you can cycle through the rows
of the parent, and then call the GetChildRows method on the row, passing the
DataRelation in. If it returns null, then there are no related records,
otherwise, copy it over.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"VMI" <vo******@yahoo.com> wrote in message
news:Ok**************@tk2msftngp13.phx.gbl...
This is the code I was initially using:

DataTable table_auditGrid = _table_auditAddress.Clone();
foreach (DataRow row in table_keys.Rows)
{
DataRow[] resultRow = _table_auditAddress.Select("col_Key = '" +
row["key"] + "'");
if (resultRow.Length > 0)
{
table_auditGrid.Rows.Add(resultRow[0]); //error saying that this
row already exists in another table
}
}

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:eU****************@TK2MSFTNGP10.phx.gbl...
VMI,

That all depends on what kind of comparison are you doing? Are you
comparing A and B to see if the same keys exist in both tables? Or,
where the keys exist in both tables, the fields are the same?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"VMI" <vo******@yahoo.com> wrote in message
news:ev**************@TK2MSFTNGP10.phx.gbl...
You're right about the query (it's been a long time since I've worked
with sql statements).
If I use this statement, where would I need to execute it in order to
compare datatable a and datatable b? Both tables are already filled with
data, and I need to store the data of the resulting set into a separate
table (which is a clone of a).

Thanks.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:ei**************@TK2MSFTNGP11.phx.gbl...
VMI,

You can't really say:

select * from A where B.key = A.key

Because you don't know what the set of B is at that point. Are you
trying to check for the existence of the key in another table? If so,
then I would just do this:

select
a.*
from
a
inner join b on a.key = b.key

Since you use the field name "key", I'm assuming that each value is
unique, and therefore a one-to-one relationship exists.

If you perform this select, it will give you the same structure as
A.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"VMI" <vo******@yahoo.com> wrote in message
news:eQ**************@TK2MSFTNGP14.phx.gbl...
>I have two tables and I want to compare these two tables with a
>query( ie. "select * from A where B.key = A.key") and the result will
>be stored in a 3rd table (table C). is this possible? If necessary, I
>can create the schema for the 3rd table (C) since it'll be exactly like
>table A. But I'm more interested in being able to store the resulting
>set into another table.
>
> Thanks.
>



Nov 16 '05 #8
VMI
Thanks for the help. It worked.

Vaughn
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:ew**************@TK2MSFTNGP15.phx.gbl...
VMI,

Ahh, I see what you are doing now. I would actually query against the
database itself, as the query would be much, much quicker to execute.

However, for what you are doing, that is the right way, with one
exception. Instead of calling the Add method on the DataRowsCollection,
you should call the ImportRow method. This will copy the row into the new
table.

Also, to make it easier (and not have to rely on the Select method),
you might want to establish a DataRelation between the two tables
(assuming they are in the same data set). Once you do that, you can cycle
through the rows of the parent, and then call the GetChildRows method on
the row, passing the DataRelation in. If it returns null, then there are
no related records, otherwise, copy it over.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"VMI" <vo******@yahoo.com> wrote in message
news:Ok**************@tk2msftngp13.phx.gbl...
This is the code I was initially using:

DataTable table_auditGrid = _table_auditAddress.Clone();
foreach (DataRow row in table_keys.Rows)
{
DataRow[] resultRow = _table_auditAddress.Select("col_Key = '" +
row["key"] + "'");
if (resultRow.Length > 0)
{
table_auditGrid.Rows.Add(resultRow[0]); //error saying that
this row already exists in another table
}
}

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:eU****************@TK2MSFTNGP10.phx.gbl...
VMI,

That all depends on what kind of comparison are you doing? Are you
comparing A and B to see if the same keys exist in both tables? Or,
where the keys exist in both tables, the fields are the same?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"VMI" <vo******@yahoo.com> wrote in message
news:ev**************@TK2MSFTNGP10.phx.gbl...
You're right about the query (it's been a long time since I've worked
with sql statements).
If I use this statement, where would I need to execute it in order to
compare datatable a and datatable b? Both tables are already filled
with data, and I need to store the data of the resulting set into a
separate table (which is a clone of a).

Thanks.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com>
wrote in message news:ei**************@TK2MSFTNGP11.phx.gbl...
> VMI,
>
> You can't really say:
>
> select * from A where B.key = A.key
>
> Because you don't know what the set of B is at that point. Are you
> trying to check for the existence of the key in another table? If so,
> then I would just do this:
>
> select
> a.*
> from
> a
> inner join b on a.key = b.key
>
> Since you use the field name "key", I'm assuming that each value is
> unique, and therefore a one-to-one relationship exists.
>
> If you perform this select, it will give you the same structure as
> A.
>
> Hope this helps.
>
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - mv*@spam.guard.caspershouse.com
>
> "VMI" <vo******@yahoo.com> wrote in message
> news:eQ**************@TK2MSFTNGP14.phx.gbl...
>>I have two tables and I want to compare these two tables with a
>>query( ie. "select * from A where B.key = A.key") and the result will
>>be stored in a 3rd table (table C). is this possible? If necessary, I
>>can create the schema for the 3rd table (C) since it'll be exactly
>>like table A. But I'm more interested in being able to store the
>>resulting set into another table.
>>
>> Thanks.
>>
>
>



Nov 16 '05 #9
VMI wrote:
I'm just coparing the keys. For any matching keys between both tables (a &
b), I'll be copying the row from table a into my new table c. So I'll be
using the query you gave me, but I wouldn't know what other instructions I'd execute it with.


For pure SQL, something like

insert into c
(
c.col1,
c.col2,
...
)
values
(
select
a.col1,
a.col2,
...
from
a t1,
b t2
where
t1.key = t2.key
);

might work.

Maybe you'll have to define the join explicitly like

from
a inner join b
on
a.key = b.key

like Nicholas suggested.

What's your database ?
Nov 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rachel Curran | last post by:
Please can anybody help me with the following: I have two separate excel spreadsheets that I have imported into access, each sheet holds the same fields. Both spreadsheets hold all information...
5
by: Megan | last post by:
Hi everybody- I'm helping a friend with a music database. She has an old one and is creating a new one. She wants to compare records and fields in the old database with records and fields in the...
8
by: Vincent | last post by:
has any one seen a program to compare mdbs'. I have ran into a few of them, but none seem to really do that job. Basically what I need to do is, take 2 access mdb's and check the differences...
5
by: Edd E | last post by:
Hi, I have a database to store my analyses (Access 2002, WinXp), the basic structure is: TABLE 1 = Sample Info TABLE 2 = Analysis type 1 TABLE 3 = Analysis type 2 TABLE 4 = Analysis type 3 ...
12
by: nyathancha | last post by:
Hi, I have a question regarding best practices in database design. In a relational database, is it wise/necessary to sometimes create tables that are not related to other tables through a...
6
by: napatel04 | last post by:
Hi everyone, I would like to know if there is a quick query someone can help me write for the following scenario. I think I can do this with VBA but since this is suppose to be a temp. solution,...
6
osward
by: osward | last post by:
Hi everyone, I have query(s) that I need to perform which I couldn't figure out, and following is the situation I have event table (which stores the event details) that has eid, date, time,...
1
by: dbanrman | last post by:
I have a module in a shopping cart program where a sellers logs in and adds product. As usual the seller logs in with an email and password which is stored in the mysql table _supplier_admins. This...
5
by: chazzy69 | last post by:
Ok heres what im trying to achive i am constantly updating the information in a table but know i what know if the data im replacing it with is new, i.e. whether or not i already had the data in the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.