<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Stored Proc on MarkJacobsen.net</title><link>https://test.markjacobsen.net/tags/stored-proc/</link><description>Recent content in Stored Proc on MarkJacobsen.net</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Mon, 15 Jul 2013 16:40:00 +0000</lastBuildDate><atom:link href="https://test.markjacobsen.net/tags/stored-proc/index.xml" rel="self" type="application/rss+xml"/><item><title>DB2 Incremental Delete</title><link>https://test.markjacobsen.net/2013/07/db2-incremental-delete/</link><pubDate>Mon, 15 Jul 2013 16:40:00 +0000</pubDate><guid>https://test.markjacobsen.net/2013/07/db2-incremental-delete/</guid><description>&lt;p&gt;Ever have to delete a lot of records from a table in DB2 and can’t truncate, but you keep filling the transaction log? One way of solving the problem without having to manually update ranges of values in a where clause is to do an incremental delete in a stored proc. Here’s an example/template to get you started…&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;WHILE EXISTS ( SELECT ITEM_ID 
		FROM XX.TABLE
		WHERE SOMETHING_NB = 1
		FETCH FIRST 1 ROWS ONLY )
DO
	DELETE FROM 
		(SELECT ROW_NUMBER() OVER() AS ROW_NB
		FROM XX.TABLE
		WHERE SOMETHING_NB = 1
		FETCH FIRST 20000 ROWS ONLY)
	WHERE ROW_NB &amp;lt;= 20000;
	
	IF ((L_SQLCODE_NB &amp;lt;&amp;gt; 0) AND (L_SQLCODE_NB &amp;lt;&amp;gt; 100)) THEN
		SIGNAL SQLSTATE '20003' SET MESSAGE_TEXT = 'Error deleting records';
	END IF; 
	
	COMMIT;
END WHILE;
&lt;/code&gt;&lt;/pre&gt;</description></item></channel></rss>