I have a table like this:
CREATE TABLE Users
(
idUser int IDENTITY(1,1) PRIMARY KEY,
firstName varchar(40) NOT NULL,
secondName varchar(40),
lastName varchar(70) NOT NULL,
position varchar(80),
section varchar(80) NOT NULL
)
And someone need to delete only one of the Users. How can I prevent first row being deleted? I can't reinsert it, first row must have idUser = 1.
You can create a Trigger on table [Users] that watch when a register from this table is being deleted. So you check if deletion is userId:1 then you don't delete it, else.. you delete it, here is the code:
CREATE TRIGGER sampleTrigger
ON [Users]
INSTEAD OF DELETE
AS
IF EXISTS(SELECT * FROM deleted WHERE idUser IN (1))
BEGIN
RAISERROR ('Is not allowed to delete idUser: 1',16, 1)
END
ELSE
BEGIN
DELETE [Users] WHERE idUser IN (SELECT idUser FROM deleted)
END
GO
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With