Solved

not exists with multiple primary keys

Posted on 2007-11-29
3
196 Views
Last Modified: 2010-04-21
Hi,
  I want to write a store procedure to delete the records in Table A not in Table B. Table A and Table B have the same primary keys. Since both of them use multiple primary keys, the following code doesn't work correctly.

delete from A
      where not exists
      (select
            B.*
       from B
       inner join A
       on
            A.k1= B.k1 and A.k2= B.k2 and
            A.k3= B.k3 and A.k4= B.k4 and A.k5= B.k5 and A.k6 = B.k6
      )

Many thanks,
0
Comment
Question by:Jinghui Li
3 Comments
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 20374463
DELETE FROM A
FROM A
LEFT OUTER JOIN B ON A.k1= B.k1 and A.k2= B.k2 and
            A.k3= B.k3 and A.k4= B.k4 and A.k5= B.k5 and A.k6 = B.k6
WHERE B.k1 IS NULL
0
 
LVL 22

Accepted Solution

by:
dportas earned 500 total points
ID: 20374558
You were very close I think, but the extra reference to A was the problem (your subquery wasn't correlated because of it).

Untested. Make sure you test this out and have a backup and before you try it for real!

delete from A
      where not exists
      (select
            B.*
       from B
       where
            A.k1= B.k1 and A.k2= B.k2 and
            A.k3= B.k3 and A.k4= B.k4 and A.k5= B.k5 and A.k6 = B.k6
      );
0
 

Author Closing Comment

by:Jinghui Li
ID: 31411684
I tried it. It works fine.
Thank you very much,
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now