<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Audit Trigger &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/audit-trigger/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dbrnd.com</link>
	<description>BIGData &#124; NoSQL &#124; MSSQL &#124; MySQL &#124; PostgreSQL</description>
	<lastBuildDate>Tue, 09 Oct 2018 19:46:44 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.4</generator>
	<item>
		<title>SQL Server: Script to find the list of Triggers</title>
		<link>https://www.dbrnd.com/2018/10/sql-server-script-to-find-the-list-of-triggers/</link>
					<comments>https://www.dbrnd.com/2018/10/sql-server-script-to-find-the-list-of-triggers/#respond</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 29 Oct 2018 16:16:28 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server DBA Script]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[Audit Trigger]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[Drop Trigger]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Error]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tuning]]></category>
		<category><![CDATA[SQL Server Programming]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[trigger]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=7415</guid>

					<description><![CDATA[In this post, I am sharing a script for getting the list of Triggers in SQL Server. Periodically, we should do the auditing of database hidden triggers. Because triggers are not good for database performance especially for OLTP system. Many times developers are creating triggers for testing purpose and later forget to drop those. SQL [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F10%2Fsql-server-script-to-find-the-list-of-triggers%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20the%20list%20of%20Triggers" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F10%2Fsql-server-script-to-find-the-list-of-triggers%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20the%20list%20of%20Triggers" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F10%2Fsql-server-script-to-find-the-list-of-triggers%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20the%20list%20of%20Triggers" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F10%2Fsql-server-script-to-find-the-list-of-triggers%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20the%20list%20of%20Triggers" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F10%2Fsql-server-script-to-find-the-list-of-triggers%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20the%20list%20of%20Triggers" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F10%2Fsql-server-script-to-find-the-list-of-triggers%2F&amp;linkname=SQL%20Server%3A%20Script%20to%20find%20the%20list%20of%20Triggers" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F10%2Fsql-server-script-to-find-the-list-of-triggers%2F&#038;title=SQL%20Server%3A%20Script%20to%20find%20the%20list%20of%20Triggers" data-a2a-url="https://www.dbrnd.com/2018/10/sql-server-script-to-find-the-list-of-triggers/" data-a2a-title="SQL Server: Script to find the list of Triggers"></a></p><p>In this post, I am sharing a script for getting the list of Triggers in SQL Server. Periodically, we should do the auditing of database hidden triggers. </p>
<p>Because triggers are not good for database performance especially for OLTP system. Many times developers are creating triggers for testing purpose and later forget to drop those. </p>
<blockquote class="wp-embedded-content" data-secret="803rZKZCdm"><p><a href="https://www.dbrnd.com/2016/10/sql-server-encrypted-ddl-trigger-to-track-all-database-changes-to-ensure-database-object-security/">SQL Server: Encrypted DDL Trigger to Track all Database Changes</a></p></blockquote>
<p><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/10/sql-server-encrypted-ddl-trigger-to-track-all-database-changes-to-ensure-database-object-security/embed/#?secret=803rZKZCdm" data-secret="803rZKZCdm" width="600" height="338" title="&#8220;SQL Server: Encrypted DDL Trigger to Track all Database Changes&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<p><strong>Execute the below script and get list of all triggers: </strong></p><pre class="crayon-plain-tag">SELECT 
	sysobjects.name AS TriggerName 
	,USER_NAME(sysobjects.uid) AS TriggerOwner 
	,s.name AS TableSchema 
	,OBJECT_NAME(parent_obj) AS TableName 
	,OBJECTPROPERTY( id, 'ExecIsUpdateTrigger') AS Isupdate 
	,OBJECTPROPERTY( id, 'ExecIsDeleteTrigger') AS Isdelete 
	,OBJECTPROPERTY( id, 'ExecIsInsertTrigger') AS Isinsert 
	,OBJECTPROPERTY( id, 'ExecIsAfterTrigger') AS Isafter 
	,OBJECTPROPERTY( id, 'ExecIsInsteadOfTrigger') AS Isinsteadof
	,OBJECTPROPERTY(id, 'ExecIsTriggerDisabled') AS Isdisabled
FROM sysobjects 
INNER JOIN sysusers 
    ON sysobjects.uid = sysusers.uid 
INNER JOIN sys.tables t 
    ON sysobjects.parent_obj = t.object_id 
INNER JOIN sys.schemas s 
    ON t.schema_id = s.schema_id 
WHERE sysobjects.type = 'TR'</pre><p> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2018/10/sql-server-script-to-find-the-list-of-triggers/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>PostgreSQL: Create Audit Trigger, for logging or Auditing your Database DML Transactions</title>
		<link>https://www.dbrnd.com/2017/04/postgresql-create-audit-trigger-for-logging-or-auditing-your-database-dml-transactions-insert-update-delete/</link>
					<comments>https://www.dbrnd.com/2017/04/postgresql-create-audit-trigger-for-logging-or-auditing-your-database-dml-transactions-insert-update-delete/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Wed, 05 Apr 2017 16:16:38 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[Audit DELETE]]></category>
		<category><![CDATA[Audit INSERT]]></category>
		<category><![CDATA[Audit Transaction]]></category>
		<category><![CDATA[Audit Trigger]]></category>
		<category><![CDATA[Audit UPDATE]]></category>
		<category><![CDATA[Data Security]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[Logging DML]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Error]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=3827</guid>

					<description><![CDATA[If you experienced with SQL Server, You might know about SQL Server Audit and Change Data Capture (CDC) functionality. As per the PostgreSQL WIKI, PostgreSQL does not have such an Inbuilt functionality for auditing your user transactions. Most of the Application requires, Database auditing like Who Inserted, When Updated kind of DML Transactions. Here, I [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F04%2Fpostgresql-create-audit-trigger-for-logging-or-auditing-your-database-dml-transactions-insert-update-delete%2F&amp;linkname=PostgreSQL%3A%20Create%20Audit%20Trigger%2C%20for%20logging%20or%20Auditing%20your%20Database%20DML%20Transactions" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F04%2Fpostgresql-create-audit-trigger-for-logging-or-auditing-your-database-dml-transactions-insert-update-delete%2F&amp;linkname=PostgreSQL%3A%20Create%20Audit%20Trigger%2C%20for%20logging%20or%20Auditing%20your%20Database%20DML%20Transactions" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F04%2Fpostgresql-create-audit-trigger-for-logging-or-auditing-your-database-dml-transactions-insert-update-delete%2F&amp;linkname=PostgreSQL%3A%20Create%20Audit%20Trigger%2C%20for%20logging%20or%20Auditing%20your%20Database%20DML%20Transactions" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F04%2Fpostgresql-create-audit-trigger-for-logging-or-auditing-your-database-dml-transactions-insert-update-delete%2F&amp;linkname=PostgreSQL%3A%20Create%20Audit%20Trigger%2C%20for%20logging%20or%20Auditing%20your%20Database%20DML%20Transactions" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F04%2Fpostgresql-create-audit-trigger-for-logging-or-auditing-your-database-dml-transactions-insert-update-delete%2F&amp;linkname=PostgreSQL%3A%20Create%20Audit%20Trigger%2C%20for%20logging%20or%20Auditing%20your%20Database%20DML%20Transactions" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F04%2Fpostgresql-create-audit-trigger-for-logging-or-auditing-your-database-dml-transactions-insert-update-delete%2F&amp;linkname=PostgreSQL%3A%20Create%20Audit%20Trigger%2C%20for%20logging%20or%20Auditing%20your%20Database%20DML%20Transactions" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F04%2Fpostgresql-create-audit-trigger-for-logging-or-auditing-your-database-dml-transactions-insert-update-delete%2F&#038;title=PostgreSQL%3A%20Create%20Audit%20Trigger%2C%20for%20logging%20or%20Auditing%20your%20Database%20DML%20Transactions" data-a2a-url="https://www.dbrnd.com/2017/04/postgresql-create-audit-trigger-for-logging-or-auditing-your-database-dml-transactions-insert-update-delete/" data-a2a-title="PostgreSQL: Create Audit Trigger, for logging or Auditing your Database DML Transactions"></a></p><p>If you experienced with SQL Server, You might know about SQL Server Audit and Change Data Capture (CDC) functionality.<br />
As per the PostgreSQL WIKI, PostgreSQL does not have such an Inbuilt functionality for auditing your user transactions. </p>
<blockquote><p>Most of the Application requires, Database auditing like Who Inserted, When Updated kind of DML Transactions.<br />
Here, I am sharing one demonstration about Trigger Approach, which is not always good, but for specific requirement, we can go with this approach. </p></blockquote>
<p>I have created separate schema and table to log about DML transactions.<br />
PostgreSQL Database Administrator can use this trigger for security purpose also. </p>
<p><strong>Create a one new Schema:</strong></p>
<pre class="crayon-plain-tag">CREATE SCHEMA AuditTable;</pre> </p>
<p><strong>Create a new table for Logged Transaction:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE AuditTable.tbl_LoggedTransactions 
(
	SchemaName CHARACTER VARYING
	,TableName CHARACTER VARYING
	,UserName CHARACTER VARYING    
	,DMLAction CHARACTER VARYING
	,OriginalData TEXT
	,ExecutedNewData TEXT
	,ExecutedSQL TEXT
	,RecordDateTime TIMESTAMP WITHOUT TIME ZONE DEFAULT NOW()
);</pre> </p>
<p><strong>Revoke all access of newly creatd table from PUBLIC: </strong></p>
<pre class="crayon-plain-tag">REVOKE ALL ON AuditTable.tbl_LoggedTransactions FROM public;</pre> </p>
<p><strong>Create one trigger function for auditing DML transactions:<br />
</strong></p>
<pre class="crayon-plain-tag">CREATE OR REPLACE FUNCTION AuditTable.trg_AuditDML() 
RETURNS TRIGGER 
AS $BODY$
DECLARE
    OldData TEXT;
    NewData TEXT;
BEGIN 
 
    IF (TG_OP = 'UPDATE') THEN
        OldData := ROW(OLD.*);
        NewData := ROW(NEW.*);
        INSERT INTO AuditTable.tbl_LoggedTransactions 
	(
		SchemaName
		,TableName
		,UserName
		,DMLAction
		,OriginalData
		,ExecutedNewData
		,ExecutedSQL
	) 
        VALUES 
	(
		TG_TABLE_SCHEMA::TEXT
		,TG_TABLE_NAME::TEXT
		,session_user::TEXT
		,substring(TG_OP,1,1)
		,OldData
		,NewData
		,current_query()
	);
        RETURN NEW;
    ELSIF (TG_OP = 'DELETE') THEN
        OldData := ROW(OLD.*);
        INSERT INTO AuditTable.tbl_LoggedTransactions 
	(
		SchemaName
		,TableName
		,UserName
		,DMLAction
		,OriginalData
		,ExecutedSQL
	)
        VALUES 
	(
		TG_TABLE_SCHEMA::TEXT
		,TG_TABLE_NAME::TEXT
		,session_user::TEXT
		,substring(TG_OP,1,1)
		,OldData
		,current_query()
	);
        RETURN OLD;
    ELSIF (TG_OP = 'INSERT') THEN
        NewData := ROW(NEW.*);
        INSERT INTO AuditTable.tbl_LoggedTransactions 
	(
		SchemaName
		,TableName
		,UserName
		,DMLAction
		,ExecutedNewData
		,ExecutedSQL
	)
        VALUES 
	(
		TG_TABLE_SCHEMA::TEXT
		,TG_TABLE_NAME::TEXT
		,session_user::TEXT
		,substring(TG_OP,1,1)
		,NewData
		,current_query()
	);
        RETURN NEW;
    ELSE
        RAISE WARNING '[AuditTable.trg_AuditDML] - Other action occurred: %, at %',TG_OP,now();
        RETURN NULL;
    END IF;
END;
$BODY$
LANGUAGE plpgsql;</pre> </p>
<p><strong>Create a sample table:<br />
</strong></p><pre class="crayon-plain-tag">CREATE TABLE public.tbl_Employees
(
	EmpID INT
	,EmpName CHARACTER VARYING 
);</pre><p> </p>
<p><strong>Create trigger on tbl_Employees with the use of trigger function AuditTable.trg_AuditDML:<br />
</strong></p>
<pre class="crayon-plain-tag">CREATE TRIGGER trg_tbl_Employees_INSERT_UPDATE_DELETE
AFTER INSERT OR UPDATE OR DELETE ON public.tbl_Employees
FOR EACH ROW EXECUTE PROCEDURE AuditTable.trg_AuditDML();</pre> </p>
<p><strong>Execute few sample DMLs:</strong></p>
<pre class="crayon-plain-tag">INSERT INTO tbl_Employees VALUES 
(1,'Anvesh'),(2,'Martin'),(3,'Roy')
,(4,'Jeeny'),(5,'Kavita'),(6,'Neevan');

UPDATE tbl_Employees SET EmpName='Mahi'
WHERE EmpID = 5;

DELETE FROM tbl_Employees WHERE EmpID = 4;</pre> </p>
<p><strong>Check AuditTable.tbl_LoggedTransactions table for logged DML transaction:<br />
</strong></p>
<pre class="crayon-plain-tag">SELECT *FROM AuditTable.tbl_LoggedTransactions;</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2017/04/postgresql-create-audit-trigger-for-logging-or-auditing-your-database-dml-transactions-insert-update-delete/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
	</channel>
</rss>
