Can you please tell me how to find the primary key column value is already exists or not in Foreign key table in sql server2008 R2.
asked Apr 23 '12 at 10:00 AM in Default
I am not sure I understand your question, but I am guessing a little:
You have table A with PK column ID and table B with column AID and you want to know if there are rows in B where AID is not in A?
answered Apr 23 '12 at 10:48 AM
The easiest way to do this that I know of would be to do a JOIN operation. You can use OUTER JOIN and look for NULL values. Something like:
You can add a WHERE clause that either filters for the parent table, Y, ID value, or just looks for IS NULL or IS NOT NULL on the child table, X, ID value.
answered Apr 23 '12 at 10:49 AM
Grant Fritchey ♦♦
A -- Primary Key table B -- Foreign Key table
Below query returns the values in primary table and which are not in foreign key table or child table.
Select * From A where ID not in (select ID from B)
If you want to check for a specified value then run the below by replace the question mark(?) with the value,
Select * From A where ID not in (select ID from B) and Id = ?
answered Apr 23 '12 at 05:08 PM