|
Hi All, 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.
(comments are locked)
|
|
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? Nice one. I spent too much time typing.
Apr 23 '12 at 10:50 AM
Grant Fritchey ♦♦
(comments are locked)
|
|
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.
(comments are locked)
|
|
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 = ?
(comments are locked)
|

