I have 2 tables. If I delete a record from table1, then the first query should check if pk exists as a foreign key in table2, then it should not delete the record that it should.
I used this error, but throwing syntax
DELETE FROM Setup.IncentivesDetail
INNER JOIN Employee.IncentivesDetail ON Setup.IncentivesDetail.IncentivesDetailID = Employee.IncentivesDetail.IncentiveDetail_ID
WHERE Setup.IncentivesDetail.IncentivesDetailID= @IncentivesDetailID
AND Employee.IncentivesDetail.IncentiveDetail_ID= @IncentivesDetailID
UPDATE:
Based on the answers below, I did this, is this correct?
If Not Exists(Select * from Employee.IncentivesDetail where IncentivesDetail.IncentiveDetail_ID= @IncentivesDetailID)
Begin
Delete from Setup.IncentivesDetail
WHERE Setup.IncentivesDetail.IncentivesDetailID= @IncentivesDetailID
End
Else
Begin
RAISERROR('Record cannot be deleted because assigned to an employee',16,1)
RETURN
End
source
share