← Tutti gli articoli

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

16 March 2011  ·  TSQL · Article  ·  564 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
Si è verificato un errore imprevisto. Ricarica

Rejoining the server...

Rejoin failed... trying again in seconds.

Failed to rejoin.
Please retry or reload the page.

The session has been paused by the server.

Failed to resume the session.
Please retry or reload the page.