<p>I have a SQL script that inserts data (via INSERT statements currently numbering in the thousands) One of the columns contains a unique identifier (though not an IDENTITY type, just a plain ol' int) that's actually unique across a few different tables. </p>

<p>I'd like to add a scalar function to my script that gets the next available ID (i.e. last used ID + 1) but I'm not sure this is possible because there doesn't seem to be a way to use a global or static variable from within a UDF, I can't use a temp table, and I can't update a permanent table from within a function. </p>

<p>Currently my script looks like this: </p>

<pre>
   declare @v_baseID int 
   exec dbo.getNextID @v_baseID out  --sproc to get the next available id
   --Lots of these - where n is a hardcoded value
   insert into tableOfStuff (someStuff, uniqueID) values ('stuff', @v_baseID + n ) 
   exec dbo.UpdateNextID @v_baseID + lastUsedn  --sproc to update the last used id
</pre>

<p>But I would like it to look like this: </p>

<pre>
   --Lots of these
   insert into tableOfStuff (someStuff, uniqueID) values ('stuff', getNextID() ) 
</pre>

<p>Hardcoding the offset is a pain in the arse, and is error prone.  Packaging it up into a simple scalar function is very appealing, but I'm starting to think it can't be done that way since there doesn't seem to be a way to maintain the offset counter between calls.  Is that right, or is there something I'm missing.  </p>

<p>We're using SQL Server 2005 at the moment. </p>

<p><em>edits for clarification:</em></p>

<p>Two users hitting it won't happen.  This is an upgrade script that will be run only once, and never concurrently. </p>

<p>The actual sproc isn't prefixed with sp_, fixed the example code. </p>

<p>In normal usage, we do use an id table and a sproc to get IDs as needed, I was just looking for a cleaner way to do it in this script, which essentially just dumps a bunch of data into the db. </p>

[{'answer_id': 28285, 'author': 'SQLMenace', 'author_id': 740, 'author_profile': 'https://Stackoverflow.com/users/740', 'pm_score': 2, 'selected': False, 'text': "<p>If you have 2 users hitting it at the same time they will get the same id. Why didn't you use an id table with an identity instead, insert into that and use that as the unique (which is guaranteed) id, this will also perform much faster</p>\n\n<blockquote>\n  <blockquote>\n    <p>sp_getNextID </p>\n  </blockquote>\n</blockquote>\n\n<p>never ever prefix procs with sp_, this has performance implication because the optimizer first checks the master DB to see if that proc exists there and then th local DB, also if MS decide to create a sp_getNextID in a service pack yours will never get executed</p>\n"}, {'answer_id': 29614, 'author': 'Kevin Crumley', 'author_id': 1818, 'author_profile': 'https://Stackoverflow.com/users/1818', 'pm_score': 0, 'selected': False, 'text': "<p>It would probably be more work than it's worth, but you can use static C#/VB variables in a SQL CLR UDF, so I think you'd be able to do what you want to do by simply incrementing this variable every time the UDF is called.  The static variable would be lost whenever the appdomain unloaded, of course.  So if you need continuity of your ID from one day to the next, you'd need a way, on first access of NextId, to poll all of tables that use this ID, to find the highest value.</p>\n"}, {'answer_id': 29660, 'author': 'Dane', 'author_id': 2929, 'author_profile': 'https://Stackoverflow.com/users/2929', 'pm_score': 3, 'selected': True, 'text': '<blockquote>\n  <p>I\'m starting to think it can\'t be done that way since there doesn\'t seem to be a way to maintain the offset counter between calls. Is that right, or is there something I\'m missing.</p>\n</blockquote>\n\n<p>You aren\'t missing anything; SQL Server does not support global variables, and it doesn\'t support data modification within UDFs.  And even if you wanted to do something as kludgy as using CONTEXT_INFO (see <a href="http://weblogs.sqlteam.com/mladenp/archive/2007/04/23/60185.aspx" rel="nofollow noreferrer">http://weblogs.sqlteam.com/mladenp/archive/2007/04/23/60185.aspx</a>), you can\'t set that from within a UDF anyway.</p>\n\n<p>Is there a way you can get around the "hardcoding" of the offset by making that a variable and looping over the iteration of it, doing the inserts within that loop?</p>\n'}]
['https://Stackoverflow.com/questions/28280', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2187/']