Tuesday, August 23, 2011

To check for more than one primary key to insert into a table.

For example table p1 and p2 are combined and inserted into table p3

INSERT INTO p1 VALUES (1000)
INSERT INTO p1 VALUES (2000)
INSERT INTO p1 VALUES (3000)
INSERT INTO p2 VALUES (1000,5)
INSERT INTO p2 VALUES (1000,2)
INSERT INTO p2 VALUES (2000,1)
INSERT INTO p2 VALUES (3000,1)

SELECT * FROM p3
INSERT INTO p3

select hdr.ponumber,ord
from p2 Det,
p1 Hdr
where Hdr.PONumber = Det.PONumber
and (det.ponumber not in (select ponumber from p3)
or det.ord not in (select ord from p3))

No comments:

Post a Comment