Reputation: 1715
Hi guys I have to update a foreign key, on a server sql 2014, but when I run the code below I have the following error, how can fix it?
Error:
Msg 3621, Level 0, State 0. The statement has been terminated. Msg 547, Level 16, State 0. The UPDATE statement conflicted with the FOREIGN KEY constraint "fk_restirizione_idrapportomobile". The conflict occurred in database "db", table "dbo.RapportoMobile", column 'IdRapportoMobile'. (Line 1)
SQL Code:
UPDATE risorsarapportomobile
SET risorsarapportomobile.idrapportomobile = 1236
WHERE risorsarapportomobile.idrisorseumane IN (SELECT
risorseumane.idrisorseumane
FROM risorsarapportomobile
INNER JOIN risorseumane
ON
risorseumane.idrisorseumane =
risorsarapportomobile.idrisorseumane
WHERE risorsarapportomobile.idrapportomobile IS NULL
AND CONVERT(VARCHAR(10), '26-06-2018', 105) =
CONVERT(VARCHAR(10), risorseumane.data, 105)
AND risorseumane.idcantiere = 158)
Tables:
Upvotes: 0
Views: 9404
Reputation: 3757
Your idRaprortoMobile
is 12
, not 1236
. 1236
is idRapporto
. Check what column your foreign key points to, and you'll see it's the issue.
Upvotes: 2
Reputation: 7240
Since you have only one field after SET, and due to the error message, we can deduce that
the value 1236 you want to enter is not present in dbo.RapportoMobile.IdRapportoMobile, which is the target of the foreign key.
Add the value to that column first, then run your update.
Upvotes: 0