Mozesz do tego tematu podejsc troche inaczej.
Zeby bylo to efektywne musisz wiedziec co chcesz hostoryzowac (czyli zmiany jakich pol Ci interesuja).
Musisz miec tez tabele w ktorej bedziesz przechowywal dane zmienione.
Proponuje utowrzyc tabele hostoryzujaca dane w oparciu o typ sql_variant
.
create table [dbo].[NazwaTabeli](
[NazwaTabeliId] [int] identity(1,1) not null,
[TrackId] [int] not null, -- referencja do opisu pola historyzowanego
[RecordId] [int] not null,
[Host] [nvarchar](256) null,
[UserId] [int] null,
[UserName] [nvarchar](100) null,
[ValueBefore] [sql_variant] null,
[ValueAfter] [sql_variant] null,
[RegisterDate] [datetime] not null,
constraint [Nazwa_PK] primary key clustered
(
[Klucz_glowny] asc
)with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary]
) on [primary]
go
alter table [dbo].[NazwaTabeli] add default (getdate()) for [RegisterDate]
go
Musisz sobie zmienic nazwe na taka jaka bedzie Tobie pasowala. A teraz tabela opisujaca historyzowane pola.
create table [dbo].[Track](
[TrackId] [int] identity(1,1) not null,
[Name] [nvarchar](256) null,
[Hint] [nvarchar](500) null,
[DatabaseObject] [sysname] not null, -- nazwa tabeli ktora bedzie historyzowana
[FieldName] [sysname] not null, -- nazwa pola ktore bedzie historyzowane
[TriggerName] [sysname] null, -- nazwa triggera historyzujacego
[DataType] [int] null, -- typ danych wg sys.types
[Enabled] [int] not null,
constraint [PK_Track] primary key clustered
(
[TrackId] asc
)with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary]
) on [primary]
go
alter table [dbo].[Track] add default ((1)) for [Enabled]
go
a teraz trigger
...... -- deklaracja naglowka na obiekcie
select * into [#inserted] from inserted -- nalezy pamietac ze pola typu blob nie moga byc tak kopiowane
select * into [#deleted] from deleted
declare @sql varchar(max);
set @sql = 'declare @update [int] = 0 ';
if (select count(*) from inserted i) = (select count(*) from deleted d)
set @sql = 'declare @update [int] = 1 ';
select @sql = @sql +
' insert into ..( ' +
' .......... -- tutaj pola ktore maja byc standardem
' ValueBefore, ' +
' ValueAfter) ' +
' select ' +
' ' + -- tutaj odpowiednio dane z tabel zrodlowych
' ' + quotename(isnull(@@ServerName, '') + ' | ' + Suser_Name(), '''') + ', ' +
' isnull(i.Modify_Id_User, d.Create_Id_User), ' +
' isnull(i.Modify_User_Name, d.Create_User_Name), ' +
' d.' + quotename(CHT.FieldName) + ', ' +
' i.' + quotename(CHT.FieldName) +
' from ' +
' [#inserted] i ' +
' left join [#deleted] d on (i.Id_Przesylki = d.Id_Przesylki) ' +
' where ' +
' (@update = 0) ' +
' or ' +
' ( ' +
' (@update = 1) and (d.' + quotename(CHT.FieldName) + ' <> ' + 'i.' + quotename(CHT.FieldName) +')' +
' ) ' + char(13) + char(10)
from
dbo.Track t
where
(t.[Enabled] = 1)
and (upper(t.DatabaseObject) = upper('dbo.Nazwa_tabeli_z_konfiguracji'))
and (upper(CHT.TriggerName) = upper('[dbo].[nazwa_tego_triggera]')) -- uzytkownik moze spbie napisac swoja wersje
if (select count(*) from dbo.Track CHT where .... dodatkowe warunki CHT.[Enabled] = 1) = 0
begin
drop table [#inserted]
drop table [#deleted]
return;
end;
execute(@sql);