Share via

Disable Foreign Key for Current Session

AlphonseG 236 Reputation points
2022-03-02T15:48:55.7+00:00

I know how to disable a Foreign Key.
My question is, can a foreign key be disabled for only the current session and still have it enforced for other users?
The simplified reason being, that it is a 2 column key, and I need to update some of the key columns in both tables. Cascade update would not suffice.

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories

0 comments No comments

Answer accepted by question author

Tom Cooper 8,501 Reputation points
2022-03-02T16:42:26.493+00:00

That's not possible. If a foreign key is disabled, it is disabled for all sessions.
If possible, your best choice might be to do your updates during a time when no other updates are being done.
Another possibility might be to create a trigger that enforces the foreign key but code the trigger to allow foreign key violations for that particular session. Then disable the foreign key. Do your updates, re-enable the foreign key, and then drop the trigger.

Tom

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.