Primary key and foreign key delete
i make a form with primary key and foreign key when i delete data it show exception but i want it show in label how to do this?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Pankaj Kumar ChoudharyRank #80 · 24.4K reputationPosted Jun 27, 2015, 1:26 AM EDT
Declare @val2 int;
DECLARE @msg [nvarchar](max);
BEGIN TRY
Set @val1=8;
Set @val2=@val1/0; /* you can put your deletion code here */
END TRY
BEGIN CATCH
SET @msg= Error_Message();
SELECT @msg; /* retrieve value from this variable */
END CATCH
Rawat104 reputationPosted Jun 27, 2015, 2:40 AM EDT
Pankaj Kumar ChoudharyRank #80 · 24.4K reputationPosted Jun 27, 2015, 3:27 AM EDT
Sujeet SumanRank #790 · 2.9K reputationPosted Jun 27, 2015, 4:15 AM EDT
Gohil JayendrasinhRank #490 · 5K reputationPosted Jun 27, 2015, 1:29 AM EDT
You can do this using CASCADE DELETE
If a record in the parent table is deleted, then the corresponding records in the child table will automatically be deleted. This is called a cascade delete in SQL Server.
Drop the existing foreign key constraint,
Add a new one with the ON DELETE CASCADE setting enabled.
ALTER TABLE dbo.T2 DROP CONSTRAINT FK_T1_T2 -- or whatever it's called
ALTER TABLE dbo.T2 ADD CONSTRAINT FK_T1_T2_Cascade
FOREIGN KEY (EmployeeID) REFERENCES dbo.T1 (EmployeeID) ON DELETE CASCADE
Than
DELETE FROM dbo.T2 WHERE EmployeeID = 1052014