print
'Create table Notes_History '
;
Create
Table
Notes_History(
ID_Notes_History
BIGINT
IDENTITY
NOT
NULL
,History_Action
varchar
(50)
,History_From
varchar
(100)
default
HOST_NAME()
,History_User
varchar
(50)
default
SYSTEM_USER
,History_Date
varchar
(50)
default
getdate()
,Id
int
NOT
NULL
,Textnote nvarchar (255)
NULL
,PersonId
int
NULL
)
print
'end Create table Notes_History '
;
GO
print
'create trigger for Notes'
GO
CREATE
TRIGGER
dbo.TR_IUP_Notes
ON
Notes
AFTER
INSERT
,
UPDATE
,
DELETE
AS
BEGIN
SET
NOCOUNT
ON
;
DECLARE
@Ins
int
DECLARE
@Del
int
SELECT
@Ins =
Count
(*)
FROM
inserted
SELECT
@Del =
Count
(*)
FROM
deleted
if(@Ins + @Del = 0)
return
;
declare
@operation
varchar
(50)
set
@operation =
'update'
;
if(@ins < @del)
set
@operation =
'delete'
;
if(@ins > @del)
set
@operation =
'insert'
;
if(@ins <= @del)
begin
INSERT
INTO
Notes_History(History_Action ,Id,Textnote,PersonId)
select
@operation ,Id,Textnote,PersonId
from
deleted
end
else
begin
INSERT
INTO
Notes_History(History_Action ,Id,Textnote,PersonId)
select
@operation ,Id,Textnote,PersonId
from
inserted
end
END