← Tutti gli articoli

Avoiding insert, update and delete a record in a table‏.

16 marzo 2011  · TSQL · Article  ·  576 visite

I need to ensure that a record is never deleted, upadated or inserted in a table.


CREATE FUNCTION IsIUP
(
--I=insert
--U=update
--D=delete
@ID int
)
RETURNS bit
AS
BEGIN
-- Declare the return variable here
DECLARE @r bit
----  .................
-- Return the result of the function
RETURN 0;
END


CREATE TRIGGER [dbo].[trgMyTableOnInsert]
ON [dbo].[MyTable]
for  INSERT
AS
declare @ID int;
select @ID=ID
from inserted;
if (@ID is not null)
begin
if( dbo.IsIUP (@ID) = 1) --My table record is locked
begin
RAISERROR ('Insert denied.', -- Message text.
20, -- Severity.
1 -- State.
);
end
end
Severity levels from 20 through 25 are considered fatal. For more information see: RAISERROR (Transact-SQL) Syntax