Not a solution for Rigger’s update, but maybe good for Criadoem:
(can vary a little from SQL dialect to another, but almost all allow things like)
When creating the table use:
CREATE TABLE T (
..definicao dos campos..
CriadoEm datetime DEFAULT CURRENT_TIMESTAMP
ModificadoEm datetime DEFAULT CURRENT_TIMESTAMP
Thus, for every new record, the Create and Modify field will get the creation date of the record if you do not enter any value.
As for future updates, there are still two possibilities:
Even use Trigger
Or, if your DB allows it, insert null into the modified fieldOn, (and set it to NOT NULL) so that it takes the default value (just testing on your specific DB to see if it works). In Mysql, for example, you should use the type TIMESTAMP
for this to happen.
Solution specific to Mysql to keep Modifidoem updated without Trigger:
CREATE TABLE t1 (
..definição dos outros campos..
ModificadoEm TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
It remains to see if the SQL you are using has things like.
Do you want a Rigger only for all tables? This is not possible, but you can generate the triggers for each table automatically.
– NullUserException
Vlw @Nulluserexception
– Tafarel Chicotti
I don’t know much about SQL Server, I don’t even know if this is supported, but in Oracle and Postgre what I used to do was to create a stored Procedure with the code and create the triggers for each table by calling that previous.
– Henrique Barcelos