<?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: Emulating nextval() function to get sequence in MySQL</title>
	<atom:link href="http://www.microshell.com/database/mysql/emulating-nextval-function-to-get-sequence-in-mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.microshell.com/database/mysql/emulating-nextval-function-to-get-sequence-in-mysql/</link>
	<description>Learn something share something</description>
	<lastBuildDate>Fri, 16 Jul 2010 20:06:22 -0700</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.1</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Maresa</title>
		<link>http://www.microshell.com/database/mysql/emulating-nextval-function-to-get-sequence-in-mysql/comment-page-1/#comment-3647</link>
		<dc:creator>Maresa</dc:creator>
		<pubDate>Fri, 30 Apr 2010 07:47:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.microshell.com/?p=777#comment-3647</guid>
		<description>@CaesarDraw Thank you. Mind letting me know what lines wasn&#039;t working? I see one that I didn&#039;t realize that somehow grater than ( &gt; ) is being printed as &gt;. It is fixed.</description>
		<content:encoded><![CDATA[<p>@CaesarDraw Thank you. Mind letting me know what lines wasn&#8217;t working? I see one that I didn&#8217;t realize that somehow grater than ( > ) is being printed as &gt;. It is fixed.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: CesarDraw</title>
		<link>http://www.microshell.com/database/mysql/emulating-nextval-function-to-get-sequence-in-mysql/comment-page-1/#comment-3594</link>
		<dc:creator>CesarDraw</dc:creator>
		<pubDate>Sat, 24 Apr 2010 21:53:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.microshell.com/?p=777#comment-3594</guid>
		<description>congrats! I corrected some lines that were causing errors and thus worked:

DELIMITER $$

DROP FUNCTION IF EXISTS `fac201001`.`nextval` $$
CREATE FUNCTION `sequence`.`nextval` (`seq_name` varchar(100)) RETURNS bigint(20) NOT DETERMINISTIC
BEGIN
  DECLARE cur_val bigint(20);


SELECT
        sequence_cur_value INTO cur_val
    FROM
        `sequence`.`sequence_data`
    WHERE
        sequence_name = seq_name;

    IF cur_val IS NOT NULL THEN
        UPDATE
            `sequence`.`sequence_data`
        SET
            sequence_cur_value = IF (
                (sequence_cur_value + sequence_increment) &gt; sequence_max_value,
                IF (
                    sequence_cycle = TRUE,
                    sequence_min_value,
                    NULL
                ),
                sequence_cur_value + sequence_increment
            )
        WHERE
            sequence_name = seq_name;
    END IF;

    RETURN cur_val;


END $$

DELIMITER ;</description>
		<content:encoded><![CDATA[<p>congrats! I corrected some lines that were causing errors and thus worked:</p>
<p>DELIMITER $$</p>
<p>DROP FUNCTION IF EXISTS `fac201001`.`nextval` $$<br />
CREATE FUNCTION `sequence`.`nextval` (`seq_name` varchar(100)) RETURNS bigint(20) NOT DETERMINISTIC<br />
BEGIN<br />
  DECLARE cur_val bigint(20);</p>
<p>SELECT<br />
        sequence_cur_value INTO cur_val<br />
    FROM<br />
        `sequence`.`sequence_data`<br />
    WHERE<br />
        sequence_name = seq_name;</p>
<p>    IF cur_val IS NOT NULL THEN<br />
        UPDATE<br />
            `sequence`.`sequence_data`<br />
        SET<br />
            sequence_cur_value = IF (<br />
                (sequence_cur_value + sequence_increment) &gt; sequence_max_value,<br />
                IF (<br />
                    sequence_cycle = TRUE,<br />
                    sequence_min_value,<br />
                    NULL<br />
                ),<br />
                sequence_cur_value + sequence_increment<br />
            )<br />
        WHERE<br />
            sequence_name = seq_name;<br />
    END IF;</p>
<p>    RETURN cur_val;</p>
<p>END $$</p>
<p>DELIMITER ;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Maresa</title>
		<link>http://www.microshell.com/database/mysql/emulating-nextval-function-to-get-sequence-in-mysql/comment-page-1/#comment-3310</link>
		<dc:creator>Maresa</dc:creator>
		<pubDate>Thu, 01 Apr 2010 04:58:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.microshell.com/?p=777#comment-3310</guid>
		<description>@okeefedp : I like the alternative way to doing this. :-) However, if you want to have sequence to increase by other than 1, or if you want a sequence max value of some number, it&#039;s not possible.

Another thing is that using one table for each sequence, plus if your sequence is large (for example the current sequence is 1M), then you essentially have a table with 1M rows. That would be overkill, I think.</description>
		<content:encoded><![CDATA[<p>@okeefedp : I like the alternative way to doing this. <img src='http://www.microshell.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  However, if you want to have sequence to increase by other than 1, or if you want a sequence max value of some number, it&#8217;s not possible.</p>
<p>Another thing is that using one table for each sequence, plus if your sequence is large (for example the current sequence is 1M), then you essentially have a table with 1M rows. That would be overkill, I think.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: okeefedp</title>
		<link>http://www.microshell.com/database/mysql/emulating-nextval-function-to-get-sequence-in-mysql/comment-page-1/#comment-3300</link>
		<dc:creator>okeefedp</dc:creator>
		<pubDate>Wed, 31 Mar 2010 05:04:34 +0000</pubDate>
		<guid isPermaLink="false">http://www.microshell.com/?p=777#comment-3300</guid>
		<description>I did it like this:

A table to represent each sequence: (In this example, a batch ID sequence)

DROP TABLE IF EXISTS BatchIDSequence;

CREATE TABLE BatchIDSequence (
  ID int(11) NOT NULL AUTO_INCREMENT
  ,processID int(11) NOT NULL
    ,PRIMARY KEY (ID)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


A NextVal stored procedure to that uses a prepared statement and accepts the sequence name as the parameter.

DROP PROCEDURE IF EXISTS pNextVal;
CREATE PROCEDURE pNextVal(in_sequenceName VARCHAR(60))
BEGIN
SET @stmt_text=CONCAT(&quot;INSERT INTO &quot;, in_sequenceName, &quot;Sequence (processID) VALUES(?)&quot;);
PREPARE stmt FROM @stmt_text;
SET @a=&quot;1&quot;;
EXECUTE stmt USING @a;
SELECT LAST_INSERT_ID();
END;


It would be called like this:

call pNextVal(&#039;BatchID&#039;);</description>
		<content:encoded><![CDATA[<p>I did it like this:</p>
<p>A table to represent each sequence: (In this example, a batch ID sequence)</p>
<p>DROP TABLE IF EXISTS BatchIDSequence;</p>
<p>CREATE TABLE BatchIDSequence (<br />
  ID int(11) NOT NULL AUTO_INCREMENT<br />
  ,processID int(11) NOT NULL<br />
    ,PRIMARY KEY (ID)<br />
) ENGINE=InnoDB DEFAULT CHARSET=latin1;</p>
<p>A NextVal stored procedure to that uses a prepared statement and accepts the sequence name as the parameter.</p>
<p>DROP PROCEDURE IF EXISTS pNextVal;<br />
CREATE PROCEDURE pNextVal(in_sequenceName VARCHAR(60))<br />
BEGIN<br />
SET @stmt_text=CONCAT(&#8221;INSERT INTO &#8220;, in_sequenceName, &#8220;Sequence (processID) VALUES(?)&#8221;);<br />
PREPARE stmt FROM @stmt_text;<br />
SET @a=&#8221;1&#8243;;<br />
EXECUTE stmt USING @a;<br />
SELECT LAST_INSERT_ID();<br />
END;</p>
<p>It would be called like this:</p>
<p>call pNextVal(&#8217;BatchID&#8217;);</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kwatog</title>
		<link>http://www.microshell.com/database/mysql/emulating-nextval-function-to-get-sequence-in-mysql/comment-page-1/#comment-1942</link>
		<dc:creator>kwatog</dc:creator>
		<pubDate>Sat, 19 Dec 2009 03:25:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.microshell.com/?p=777#comment-1942</guid>
		<description>whoa! this is an excellent alternative. however, I wish mysql will create a similar function.</description>
		<content:encoded><![CDATA[<p>whoa! this is an excellent alternative. however, I wish mysql will create a similar function.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Maresa</title>
		<link>http://www.microshell.com/database/mysql/emulating-nextval-function-to-get-sequence-in-mysql/comment-page-1/#comment-999</link>
		<dc:creator>Maresa</dc:creator>
		<pubDate>Sun, 18 Oct 2009 04:53:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.microshell.com/?p=777#comment-999</guid>
		<description>Good question. I vaguely remember testing concurrency and found out that if you have 2 concurrent statements, the second one is in blocked state until the first one finished executing. I tested it by adding &lt;a href=&quot;http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_sleep&quot; rel=&quot;nofollow&quot;&gt;sleep()&lt;/a&gt; function and call nextval() from 2 different connections.

However, to be even more safe, I guess I can change the sequence table to use InnoDB and do SELECT FOR UPDATE on the stored procedure. Off course it&#039;ll be within transaction.</description>
		<content:encoded><![CDATA[<p>Good question. I vaguely remember testing concurrency and found out that if you have 2 concurrent statements, the second one is in blocked state until the first one finished executing. I tested it by adding <a href="http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_sleep" rel="nofollow">sleep()</a> function and call nextval() from 2 different connections.</p>
<p>However, to be even more safe, I guess I can change the sequence table to use InnoDB and do SELECT FOR UPDATE on the stored procedure. Off course it&#8217;ll be within transaction.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: nonymous</title>
		<link>http://www.microshell.com/database/mysql/emulating-nextval-function-to-get-sequence-in-mysql/comment-page-1/#comment-996</link>
		<dc:creator>nonymous</dc:creator>
		<pubDate>Sun, 18 Oct 2009 01:27:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.microshell.com/?p=777#comment-996</guid>
		<description>How do guarantee - once you get 100 clients beating on your database - that you won&#039;t get the same sequence ID returned to multiple clients? 
If your mysql server is running on a multi-processor system, you may VERY WELL have the nextval function being ran twice simultaneously. Two clients will read the SAME value for sequence_cur_value...</description>
		<content:encoded><![CDATA[<p>How do guarantee &#8211; once you get 100 clients beating on your database &#8211; that you won&#8217;t get the same sequence ID returned to multiple clients?<br />
If your mysql server is running on a multi-processor system, you may VERY WELL have the nextval function being ran twice simultaneously. Two clients will read the SAME value for sequence_cur_value&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Luiz Weber TI</title>
		<link>http://www.microshell.com/database/mysql/emulating-nextval-function-to-get-sequence-in-mysql/comment-page-1/#comment-973</link>
		<dc:creator>Luiz Weber TI</dc:creator>
		<pubDate>Thu, 15 Oct 2009 13:01:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.microshell.com/?p=777#comment-973</guid>
		<description>Great job!!!

I just added a few lines before closing the IF to enter the sequence automatically failing:

[...]
 WHERE
            sequence_name = seq_name
        ;
	INSERT INTO
            sequence.sequence_data
        SET
            sequence_cur_value = 0,
            sequence_name = seq_name;   
    END IF;
[...]</description>
		<content:encoded><![CDATA[<p>Great job!!!</p>
<p>I just added a few lines before closing the IF to enter the sequence automatically failing:</p>
<p>[...]<br />
 WHERE<br />
            sequence_name = seq_name<br />
        ;<br />
	INSERT INTO<br />
            sequence.sequence_data<br />
        SET<br />
            sequence_cur_value = 0,<br />
            sequence_name = seq_name;<br />
    END IF;<br />
[...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Maresa</title>
		<link>http://www.microshell.com/database/mysql/emulating-nextval-function-to-get-sequence-in-mysql/comment-page-1/#comment-767</link>
		<dc:creator>Maresa</dc:creator>
		<pubDate>Wed, 23 Sep 2009 06:28:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.microshell.com/?p=777#comment-767</guid>
		<description>Thank you Gopi. Glad it&#039;s useful for you.</description>
		<content:encoded><![CDATA[<p>Thank you Gopi. Glad it&#8217;s useful for you.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gopi Srinivas</title>
		<link>http://www.microshell.com/database/mysql/emulating-nextval-function-to-get-sequence-in-mysql/comment-page-1/#comment-766</link>
		<dc:creator>Gopi Srinivas</dc:creator>
		<pubDate>Wed, 23 Sep 2009 05:21:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.microshell.com/?p=777#comment-766</guid>
		<description>This article is pretty good and was very helpful in creating sequence implementation in MySQL. We have done the implementation in one of our application.</description>
		<content:encoded><![CDATA[<p>This article is pretty good and was very helpful in creating sequence implementation in MySQL. We have done the implementation in one of our application.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
