>>>Hi,
>>>
>>>I am struggling with creating the following SQL UPDATE:
>>>
>>>I have table PART_LIST with the PK field PART_PK (int) and unique field PART_NO (char).
>>>Another table is PART_AUDIT with FK PART_PK and FK PART_NO.
>>>I want to update PART_AUDIT column PART_PK with NULL for those records that do not have an entry of PART_NO in the PART_LIST. That is, before I can set the FK constraint on PART_PK I need to eliminate all the invalid keys.
>>>
>>>I would appreciate any suggestions or some sample code.
>>>
>>>This is my attempt at the code
>>>
>>>
>>>update PART_AUDIT set PART_PK = null where not exists (select PART_PK from PART_LIST where
>>> PART_LIST.PART_NO = PART_AUDIT.PART_NO)
>>>
>>
>>
>>A follow up question. Do you (all) think my code would work as well if I delete the records that do not "belong"? E.g. if I change my code to:
>>
>>
>> DELETE FROM PART_AUDIT where not exists (select PART_PK from PART_LIST where
>> PART_LIST.PART_NO = PART_AUDIT.PART_NO)
>>
>
>You may try:
>
>
>DELETE PART_AUDIT FROM PART_AUDIT where not exists (select 1 from PART_LIST where
> PART_LIST.PART_NO = PART_AUDIT.PART_NO)
>
>I think your code may also work, but this code will work 100%.
Thank you very much!
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham