 




<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: How to use SQL triggers to log errors in a SQL table?</title>
	<atom:link href="http://itknowledgeexchange.techtarget.com/itanswers/error-log-in-ms-sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://itknowledgeexchange.techtarget.com/itanswers/error-log-in-ms-sql/</link>
	<description></description>
	<lastBuildDate>Sun, 19 May 2013 03:14:28 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
	<item>
		<title>By: mrdenny</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/error-log-in-ms-sql/#comment-58684</link>
		<dc:creator>mrdenny</dc:creator>
		<pubDate>Wed, 17 Dec 2008 20:02:37 +0000</pubDate>
		<guid isPermaLink="false">#comment-58684</guid>
		<description><![CDATA[In T/SQL we simply roll back the transaction then insert the logging data into the log table.

&lt;pre&gt;BEGIN TRANSACTION
BEGIN TRY
    Some statement which fails.
END TRY
BEGIN CATCH

ROLLBACK

INSERT INTO LogTable
(ErrorMessage, ErrorNumber, ...)
SELECT ERROR_MESSAGE(), ERROR_NUMBER(), ...

END CATCH

IF @@TRANCOUNT &lt;&gt; 0
COMMIT&lt;/pre&gt;]]></description>
		<content:encoded><![CDATA[<p>In T/SQL we simply roll back the transaction then insert the logging data into the log table.</p>
<pre>BEGIN TRANSACTION
BEGIN TRY
    Some statement which fails.
END TRY
BEGIN CATCH

ROLLBACK

INSERT INTO LogTable
(ErrorMessage, ErrorNumber, ...)
SELECT ERROR_MESSAGE(), ERROR_NUMBER(), ...

END CATCH

IF @@TRANCOUNT &lt;&gt; 0
COMMIT</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: kccrosser</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/error-log-in-ms-sql/#comment-58618</link>
		<dc:creator>kccrosser</dc:creator>
		<pubDate>Tue, 16 Dec 2008 01:33:10 +0000</pubDate>
		<guid isPermaLink="false">#comment-58618</guid>
		<description><![CDATA[The primary &quot;gotcha&quot; with trying to record errors into a database table is that the error is normally followed by a rollback of the transaction, which then discards the error record(s), since they are being written implicitly as a part of the overall transaction.

Oracle provides a very useful system package - dbms.alerts - that allows creating a nice solution to this problem.  I am not a SQL Server guru, so I won&#039;t try to give the SQL Server equivalent, but will describe how the package is used.

The Oracle Alerts package allows you to create a &quot;listener&quot; process, which is thus detached from the &quot;sender&quot; process, and then send/post an Alert from one process to the other.  A &quot;side effect&quot; of Alerts is that once you post an alert, it is not affected by subsequent commits or rollbacks - it is like sending out an e-mail.  Further, Alerts is an automatic FIFO queue, so we didn&#039;t have to worry about process timings.

Thus, we created a simple background Oracle job (the &quot;logger&quot;) that started up and &quot;listened&quot; for Alerts on a specific virtual pipe.  When it received an alert, it wrote the contents into a database log table as a single record transaction and a commit, then went back to listen for another one.

Minor technical note - we actually had a public package that the &quot;senders&quot; called, which generated some housekeeping stuff.  The &quot;sender&quot; usually called with 4 parameters - the SQL error number (or 0 for non-error logs), the SQL error text (or null), the &quot;type&quot; of the log (&quot;E&quot; for error, &quot;L&quot; for general logs, &quot;S&quot; for status, &quot;D&quot; for debug, etc.), and the current &quot;procedure name&quot; (i.e., stored procedure or function name).  The public shared package took those parameters and added a number of system information fields - the date/time of the record, the process id, the workstation/terminal name, the user information from the process, etc.  Then, when the logger wrote the data, it also added a unique sequence number.

Then, in all of our normal transactions, when there was something we wanted to record (particularly in the error handlers), we called the log package with the simple parameters, and it just copied the information into a simple message structure and posted the data as an Alert to the listener job.

Voila - this made it trivial to instrument our transactions to record errors (and debug/status info) in SQL transactions without disrupting the actual transaction.

Any SQL Server expert who knows of a similar function, please feel free to discuss how to implement a similar logger...  I would be interested to know what corresponding facilities exist in SQL Server.]]></description>
		<content:encoded><![CDATA[<p>The primary &#8220;gotcha&#8221; with trying to record errors into a database table is that the error is normally followed by a rollback of the transaction, which then discards the error record(s), since they are being written implicitly as a part of the overall transaction.</p>
<p>Oracle provides a very useful system package &#8211; dbms.alerts &#8211; that allows creating a nice solution to this problem.  I am not a SQL Server guru, so I won&#8217;t try to give the SQL Server equivalent, but will describe how the package is used.</p>
<p>The Oracle Alerts package allows you to create a &#8220;listener&#8221; process, which is thus detached from the &#8220;sender&#8221; process, and then send/post an Alert from one process to the other.  A &#8220;side effect&#8221; of Alerts is that once you post an alert, it is not affected by subsequent commits or rollbacks &#8211; it is like sending out an e-mail.  Further, Alerts is an automatic FIFO queue, so we didn&#8217;t have to worry about process timings.</p>
<p>Thus, we created a simple background Oracle job (the &#8220;logger&#8221;) that started up and &#8220;listened&#8221; for Alerts on a specific virtual pipe.  When it received an alert, it wrote the contents into a database log table as a single record transaction and a commit, then went back to listen for another one.</p>
<p>Minor technical note &#8211; we actually had a public package that the &#8220;senders&#8221; called, which generated some housekeeping stuff.  The &#8220;sender&#8221; usually called with 4 parameters &#8211; the SQL error number (or 0 for non-error logs), the SQL error text (or null), the &#8220;type&#8221; of the log (&#8220;E&#8221; for error, &#8220;L&#8221; for general logs, &#8220;S&#8221; for status, &#8220;D&#8221; for debug, etc.), and the current &#8220;procedure name&#8221; (i.e., stored procedure or function name).  The public shared package took those parameters and added a number of system information fields &#8211; the date/time of the record, the process id, the workstation/terminal name, the user information from the process, etc.  Then, when the logger wrote the data, it also added a unique sequence number.</p>
<p>Then, in all of our normal transactions, when there was something we wanted to record (particularly in the error handlers), we called the log package with the simple parameters, and it just copied the information into a simple message structure and posted the data as an Alert to the listener job.</p>
<p>Voila &#8211; this made it trivial to instrument our transactions to record errors (and debug/status info) in SQL transactions without disrupting the actual transaction.</p>
<p>Any SQL Server expert who knows of a similar function, please feel free to discuss how to implement a similar logger&#8230;  I would be interested to know what corresponding facilities exist in SQL Server.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mrdenny</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/error-log-in-ms-sql/#comment-58574</link>
		<dc:creator>mrdenny</dc:creator>
		<pubDate>Sat, 13 Dec 2008 00:34:59 +0000</pubDate>
		<guid isPermaLink="false">#comment-58574</guid>
		<description><![CDATA[Check out my SQL Server blog &quot;&lt;a href=&quot;http://itknowledgeexchange.techtarget.com/sql-server/&quot;&gt;SQL Server with Mr Denny&lt;/a&gt;&quot; for more SQL Server information.]]></description>
		<content:encoded><![CDATA[<p>Check out my SQL Server blog &#8220;<a href="http://itknowledgeexchange.techtarget.com/sql-server/">SQL Server with Mr Denny</a>&#8221; for more SQL Server information.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using memcached
Database Caching 3/10 queries in 0.034 seconds using memcached
Object Caching 295/301 objects using memcached

Served from: itknowledgeexchange.techtarget.com @ 2013-05-19 22:52:37 -->