<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[My Visual Database — audit]]></title>
		<link>https://myvisualdatabase.com/forum/viewtopic.php?id=8728</link>
		<atom:link href="https://myvisualdatabase.com/forum/extern.php?action=feed&amp;tid=8728&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[The most recent posts in audit.]]></description>
		<lastBuildDate>Sat, 07 Oct 2023 18:33:21 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[audit]]></title>
			<link>https://myvisualdatabase.com/forum/viewtopic.php?pid=49164#p49164</link>
			<description><![CDATA[<p>Based on code snippets taken from the forum and what I learned here, below is the script I am using to audit changes to a given database table. Works 100%</p><div class="codebox"><pre><code>procedure triggers_employee;
//*** Proc 845 - auditar alterações em tabela
begin
    SQLExecute(&#039;DROP TRIGGER IF EXISTS log_insert_employee&#039;); // how to remove trigger
    SQLExecute(&#039;DROP TRIGGER IF EXISTS log_update_employee&#039;); // how to remove trigger
    SQLExecute(&#039;DROP TRIGGER IF EXISTS log_delete_employee&#039;); // how to remove trigger


    // trigger when insert new a record
    SQLExecute(
    &#039;CREATE TRIGGER IF NOT EXISTS log_insert_employee AFTER INSERT ON employees &#039;+
    &#039;BEGIN &#039;+
    &#039;    INSERT INTO tbl_auditoria_employee(employee_id,  new_lastname, new_firstname, new_dateofbirth, ocorrencia, status, nome_user) VALUES &#039;+
    &#039;    (NEW.id, NEW.lastname, NEW.firstname, NEW.dateofbirth,datetime(&#039;&#039;now&#039;&#039;, &#039;&#039;localtime&#039;&#039;), &#039;&#039;New&#039;&#039;,&quot;&#039;+nome_user+&#039;&quot;); &#039;+
    &#039;END;&#039;
    );

    // trigger when update a record
    SQLExecute(
    &#039;CREATE TRIGGER IF NOT EXISTS log_update_employee AFTER UPDATE ON employees &#039;+
    &#039;BEGIN &#039;+
    &#039;    INSERT INTO tbl_auditoria_employee(employee_id, old_lastname, old_firstname, old_dateofbirth, new_lastname, new_firstname, new_dateofbirth, ocorrencia,status, nome_user) VALUES &#039;+
    &#039;    (NEW.id, OLD.lastname, OLD.firstname, OLD.dateofbirth, NEW.lastname, NEW.firstname, NEW.dateofbirth,datetime(&#039;&#039;now&#039;&#039;, &#039;&#039;localtime&#039;&#039;), &#039;&#039;Update&#039;&#039;,&quot;&#039;+nome_user+&#039;&quot;); &#039;+
    &#039;END;&#039;
    );

    // trigger when delete a record
    SQLExecute(
    &#039;CREATE TRIGGER IF NOT EXISTS log_delete_employee AFTER DELETE ON employees &#039;+
    &#039;BEGIN &#039;+
    &#039;    INSERT INTO tbl_auditoria_employee(employee_id, old_lastname, old_firstname, old_dateofbirth, ocorrencia,status, nome_user) VALUES &#039;+
    &#039;    (OLD.id, OLD.lastname, OLD.firstname,OLD.dateofbirth, datetime(&#039;&#039;now&#039;&#039;, &#039;&#039;localtime&#039;&#039;), &#039;&#039;Delete&#039;&#039;,&quot;&#039;+nome_user+&#039;&quot;); &#039;+
    &#039;END;&#039;
    );
end;

procedure Form1_OnShow (Sender: TObject; Action: string);
begin
    triggers_employee; //*** Proc 845 - auditar alterações em tabela
end;</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (jrga)]]></author>
			<pubDate>Sat, 07 Oct 2023 18:33:21 +0000</pubDate>
			<guid>https://myvisualdatabase.com/forum/viewtopic.php?pid=49164#p49164</guid>
		</item>
	</channel>
</rss>
