I'm tearing my hair out over a couple of simple queries.
I have 2 tables that I need to work with.
One is a production table and the other is an import table.
The stored procedure needs to join the 2 tables on the first 3 fields and if it finds a match then update the production table with the data from the import table.
If the first 3 fields do not match then insert the record from the import table into the production table.
This was the easy part.
If only one or two of the fields match, then the import record is an exception and needs to be inserted into a third table.
The matching fields are item_no, upc, and source. These are all varchar fields and are named the same in both tables.
I created 2 test tables and some test queries to show how I think it should work.
I can't figure out how to get what's in between.
I need the partial matches and I keep getting the others mixed in.
I'm looking for ALL, NOTHING, and INBETWEEN.
Any help would be really great. I've search and worked on this for 2 days.
Thanks so much.
I have 2 tables that I need to work with.
One is a production table and the other is an import table.
The stored procedure needs to join the 2 tables on the first 3 fields and if it finds a match then update the production table with the data from the import table.
If the first 3 fields do not match then insert the record from the import table into the production table.
This was the easy part.
If only one or two of the fields match, then the import record is an exception and needs to be inserted into a third table.
The matching fields are item_no, upc, and source. These are all varchar fields and are named the same in both tables.
I created 2 test tables and some test queries to show how I think it should work.
Code:
TableA
C1 C2 C3
-------------------
1 A A1
2 B A2
3 C A3
4 D A4
5 E A5
6 F A6
Code:
TableB
C1 C2 C3
-------------------
1 A A1
2 Z A2
3 D ZZ
8 E A5
9 Z 6
4 D ZZ
Code:
-- ONLY RETURN NEW RECORDS (NO MATCHES)
SELECT b.c1,b.c2,b.c3, a.c1,a.c2,a.c3
FROM TableB b
left outer join TableA a
on
b.c1 = a.c1
and
b.c2 = a.c2
and
b.c3 = a.c3
where
a.c1 is not null
and
a.c2 is not null
and
a.c3 is not null
Code:
-- ONLY RETURN NON NEW RECORDS
SELECT b.c1[b.c1],b.c2[b.c2],b.c3[b.c3],a.c1[a.c1],a.c2[a.c2],a.c3[a.c3]
FROM TableB b
left outer join TableA a
on
b.c1 = a.c1
and
b.c2 = a.c2
and
b.c3 = a.c3
where
a.c1 is null
and
a.c2 is null
and
a.c3 is null
I can't figure out how to get what's in between.
I need the partial matches and I keep getting the others mixed in.
I'm looking for ALL, NOTHING, and INBETWEEN.
Any help would be really great. I've search and worked on this for 2 days.
Thanks so much.