Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 non-matching rows

Author  Topic 

sqlbug
Posting Yak Master

201 Posts

Posted - 2008-12-03 : 16:22:09
I used a query to Load values from two tables based on matching column values - to another table.
It is suppossed to get 100% rows from one of the tables to my new table.
However, some rows are missing.
I wanted to see which one's. and need your help on this. Here's my sample query used for the insert:

INSERT INTO ABC(......)
SELECT FROM X_INFO, Y_INFO
WHERE X_ZONE = Y_ZONE AND X_PLOT = Y_PLOT

Thanks.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-12-03 : 16:23:44
You've got orphaned rows. Use a left outer join and check which column has null data to see where the orphans are. I take it you don't have a foreign key constraint here?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2008-12-03 : 16:34:49
ABC was your target table.
X_INFO was your source table with 100% rows needed.
Y_INFO was your second source table and there are some rows not matching to X_INFO.

You have used an inner join and this has taken only matching rows of the two tables.

Now you want to know which rows from X_INFO are not in ABC?

Select * from X_INFO as X where not exists (select * from ABC where matching_column = X.matching_column)

Webfred


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

sqlbug
Posting Yak Master

201 Posts

Posted - 2008-12-03 : 17:00:22
Thanks to both of you - Webfred, and Tara. It was very helpful.
I was trying using not exists, but doing something wrong - resulting in bad data.
Go to Top of Page
   

- Advertisement -