Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could leverage SQL Server's <a href="http://technet.microsoft.com/en-us/library/ms186775.aspx" rel="nofollow">IDENTITY</a> mechanism for this, because it's easy to use and handles concurrency well.</p> <p>More specifically, you could create three tables that contain only an identity (auto-incremented) <code>Id</code> column, using this script:</p> <pre><code>create table RedCardIds(Id int identity(1, 1) primary key) create table BlueCardIds(Id int identity(50001, 1) primary key) create table GreenCardIds(Id int identity(100001, 1) primary key) GO </code></pre> <p>The three tables have their identity values set to match your interval lower bounds.</p> <p>Then, for every request you'd make an insert into the appropriate table and use <a href="http://technet.microsoft.com/en-us/library/ms177564.aspx" rel="nofollow">the OUTPUT clause</a> to get the newly generated identity value. </p> <p>For example, if the request was for a Red Card, you could write:</p> <pre><code>insert RedCardIds output inserted.Id default values </code></pre> <p>which would output:</p> <pre class="lang-none prettyprint-override"><code>Id ----------- 1 (1 row(s) affected) </code></pre> <p>On the next run it'll return <code>2</code>, and so on.</p> <p>Similarly, the first request for a Blue Card would trigger the statement:</p> <pre><code>insert BlueCardIds output inserted.Id default values </code></pre> <p>with the result:</p> <pre class="lang-none prettyprint-override"><code>Id ----------- 500001 (1 row(s) affected) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload