I believe that in this way you will be able to achieve what you want:
CREATE TABLE [log]
(
Id UNIQUEIDENTIFIER CONSTRAINT log_Id_DF DEFAULT(NEWID()) NOT NULL
, [Timestamp] DATETIME CONSTRAINT log_Timestamp_DF DEFAULT(GETDATE()) NOT NULL
, Tabela NVARCHAR(255) NOT NULL
, Colunas NVARCHAR(MAX) NOT NULL
, PRIMARY KEY (Id)
)
DECLARE @SQL AS NVARCHAR(MAX) = ''
DECLARE cur_trigger CURSOR FOR
SELECT '
CREATE TRIGGER [' + TABLE_SCHEMA + '].[tr' + TABLE_NAME + '_auto]
ON [' + TABLE_SCHEMA + '].[' + TABLE_NAME + '] FOR INSERT, UPDATE, DELETE AS
BEGIN
DECLARE @IdTabela INT
DECLARE @Tabela NVARCHAR(255)
DECLARE @Colunas NVARCHAR(MAX)
SELECT @Tabela = object_name(parent_id)
FROM sys.triggers
WHERE object_id = @@PROCID
SELECT @IdTabela = T.id
FROM sysobjects P JOIN sysobjects T ON P.parent_obj = T.id
WHERE P.id = @@PROCID
SELECT @Colunas = ISNULL(@Colunas + '', '', '''') + name
FROM syscolumns
WHERE id = @IdTabela
AND CONVERT(VARBINARY, REVERSE(COLUMNS_UPDATED())) & POWER(CONVERT(BIGINT, 2), colorder - 1) > 0
INSERT INTO [log](Tabela, Colunas) VALUES(@Tabela, @Colunas)
END
'
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
AND TABLE_NAME <> 'log'
ORDER BY TABLE_NAME
OPEN cur_trigger
FETCH NEXT FROM cur_trigger INTO @SQL
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC(@SQL)
FETCH NEXT FROM cur_trigger INTO @SQL
END
CLOSE cur_trigger
DEALLOCATE cur_trigger
Basically uses dynamic SQL to build the trigger
and then all you have to do is execute.
In terms of syntax is correct, but has not been tested in your scenario.
Some ideas taken from Soen: SQL Server Update Trigger, Get Only modified Fields
To consult all the triggers
from a database, you can do the following:
SELECT *
FROM sys.triggers
WHERE [type] = 'TR'
ORDER BY [name]
John, I’ll test as soon as I can and get back to you! Only one case that Woss raised, the table "log" can not enter the creation of triggers, if it would not become eternal loop... rs
– rbz
And it doesn’t come in @Rbz, it’s before the
CURSOR
:). Anyway I just put it to be a 100% functional example, but of course, the table is later created as you want.– João Martins
Oops! Top! I need to look very calmly to digest all this! kkkk From what I was seeing, it will list up the changed columns?
– rbz
Yes, that’s it! All you need to do is test it in your database to confirm that triggers are well created and are executed correctly.
– João Martins
There is a mistake in the creation of the log, in the type... but I will test in general and I tell you. Thanks teacher!
– rbz
You’re right, a
DEFAULT CONSTRAINT
in columns of the typeTIMESTAMP
. Edited response!– João Martins
Let’s go continue this discussion in chat.
– rbz
John, I excluded everything, I executed again, and the table
log
is generating Tigger yes...– rbz
But the table itself is supposed to,
log
, generate records in log? 'Cause if you don’t, we’re gonna have to put a clause in to prevent that.– João Martins
But then goes into infinite loop João. If Trigger inserts in
log
any DML, she would always "auto-fire". I think aAND TABLE_NAME <> 'log'
would solve, right?– rbz
Right, yes! I’ll edit the answer to include this scenario.
– João Martins
Working perfectly!
– rbz