Tuesday, March 27, 2012

Comparing Tables

If I have Table A and Table B and would like to compare the records in both
tables and take the missing records from A and add them to table B. How can
I do this?
Table A and Table B are different but they have one field in common.
ThanksNiles wrote:
> If I have Table A and Table B and would like to compare the records
> in both tables and take the missing records from A and add them to
> table B. How can I do this?
> Table A and Table B are different but they have one field in common.
> Thanks
Assuming they have keys that can be joined (I assume this is what you
mean by having one column in common).
create table #A (col1 int)
create table #B (col1 int)
insert into #A values (1)
insert into #A values (2)
insert into #A values (3)
insert into #B values (1)
insert into #B values (3)
Select * from #B
Insert Into #B (col1)
Select col1
From #A
Where Not Exists (
Select * from #B Where #B.col1 = #A.col1)
Select * from #B
David Gugick
Imceda Software
www.imceda.com|||This query will do it...
INSERT INTO B (COLUMN1,COLUMN2)
SELECT (COLUMN1,COLUMN2)
FROM A
WHERE NOT EXISTS
(SELECT *
FROM B
WHERE A.COMMON_FIELD = B.COMMON_FIELD)
Arshad
arshadmd@.gmail.com
"Niles" wrote:
> If I have Table A and Table B and would like to compare the records in both
> tables and take the missing records from A and add them to table B. How can
> I do this?
> Table A and Table B are different but they have one field in common.
> Thanks|||Well, I am trying to insert just the primary key From A, the rest of the
columns I want to enter another value such as 0 or some comment. How do I do
that?
"Arshad" wrote:
> This query will do it...
> INSERT INTO B (COLUMN1,COLUMN2)
> SELECT (COLUMN1,COLUMN2)
> FROM A
> WHERE NOT EXISTS
> (SELECT *
> FROM B
> WHERE A.COMMON_FIELD = B.COMMON_FIELD)
> Arshad
> arshadmd@.gmail.com
> "Niles" wrote:
> > If I have Table A and Table B and would like to compare the records in both
> > tables and take the missing records from A and add them to table B. How can
> > I do this?
> > Table A and Table B are different but they have one field in common.
> > Thankssqlsql

No comments:

Post a Comment