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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Pankaj Kumar ChoudharyPosted Jun 27, 2015, 5:26 AM
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
Sujeet SumanPosted Jun 27, 2015, 8:15 AM
Pankaj Kumar ChoudharyPosted Jun 27, 2015, 7:27 AM
RawatPosted Jun 27, 2015, 6:40 AM
Gohil JayendrasinhPosted Jun 27, 2015, 5:29 AM
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