Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7500993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:23:01+00:00 2026-05-29T20:23:01+00:00

Given the following procedure call which returns an application-managed unique integer: DECLARE @id as

  • 0

Given the following procedure call which returns an application-managed unique integer:

DECLARE @id as integer
DECLARE @num_ids as integer
EXEC dbo.i13_get_ids 2, 1, @id output, @num_ids output
SELECT  @id as N'@base_id'

I’m passing the result id onto an insert statement

insert into dbo.TBL_LOCATIONS (objectid, X_Coord, Y_Coord, Loc_Name)
values (@id,296163,3961644, 'new test')

However, that can be a bit ominous when iserting 2 or more rows, as I’ve done here:

DECLARE @id as integer
DECLARE @num_ids as integer
EXEC dbo.i13_get_ids 2, 1, @id output, @num_ids output
SELECT  @id as N'@base_id';

insert into dbo.TBL_LOCATIONS (objectid, X_Coord, Y_Coord, Loc_Name)
values (@id,296163,3961644, 'new test');

EXEC dbo.i13_get_ids 2, 1, @id output, @num_ids output
SELECT  @id as N'@base_id';

insert into dbo.TBL_LOCATIONS (objectid, X_Coord, Y_Coord, Loc_Name)
values (@id,296163,3961644, 'new test2');

EXEC dbo.i13_get_ids 2, 1, @id output, @num_ids output
SELECT  @id as N'@base_id';

insert into dbo.TBL_LOCATIONS (objectid, X_Coord, Y_Coord, Loc_Name)
values (@id,296563,3961644, 'new test3');

Surely there has to be a more efficient way?

Here is the procedure that is retrieving the next ID:

GO

/****** Object:  StoredProcedure [dbo].[i13_get_ids]  ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[i13_get_ids]
@id_type integer,
@num_requested_ids integer,
@base_id integer OUTPUT,
@num_obtained_ids integer OUTPUT AS SET NOCOUNT ON
BEGIN
  IF (@num_requested_ids < 0)
  BEGIN 
    BEGIN TRAN id_tran
    /* We are resetting the generator. */
    /* Delete fragments and update the base value.*/
    UPDATE ATBI.DBO.i13 WITH  (tablockx, holdlock)
      SET base_id = base_id + @num_requested_ids
      WHERE num_ids = -1 AND id_type = @id_type
    DELETE FROM ATBI.DBO.i13 WHERE id_type = @id_type and num_ids <> -1
    COMMIT TRAN id_tran /* releases holdlock table lock */
  END
  ELSE
  BEGIN
    IF (@num_requested_ids > 0)
    BEGIN
      DECLARE I_cursor CURSOR FOR
        SELECT base_id, num_ids
        FROM ATBI.DBO.i13 WITH (tablockx, holdlock)
        WHERE id_type = @id_type
        ORDER BY num_ids DESC /* ensures that fragments come first */
        FOR UPDATE /* to get a lock */
    END
    ELSE
    BEGIN
      /* only interested in base id */
      DECLARE I_cursor CURSOR FOR
        SELECT base_id, num_ids
        FROM ATBI.DBO.i13 WITH (tablockx, holdlock)
        WHERE id_type = @id_type AND num_ids = -1
        FOR UPDATE /* to get a lock */
    END
    BEGIN TRAN id_tran
    OPEN I_cursor
    FETCH NEXT FROM I_cursor INTO @base_id, @num_obtained_ids
    IF (@num_requested_ids = 0)
    BEGIN
      /* Just getting current value */
      SET @num_obtained_ids = 0
    END
    ELSE
    BEGIN
      IF (@num_obtained_ids = -1)
      BEGIN
        /* user got the amount they wanted */
        SET @num_obtained_ids = @num_requested_ids
        /* update the last id and base id */
        UPDATE ATBI.DBO.i13
          SET base_id = base_id + @num_obtained_ids,
              last_id =  @base_id
          WHERE CURRENT OF I_cursor
      END
      ELSE
      BEGIN
        /* user got a fragment */
        IF (@num_requested_ids = 1) AND (@num_obtained_ids > 1)
        BEGIN
          /* they want one and exactly one id */
          SET @num_obtained_ids = 1
          UPDATE ATBI.DBO.i13 SET base_id = base_id + 1,
            num_ids =  num_ids - 1 WHERE CURRENT OF I_cursor
        END
        ELSE
        BEGIN
          /* Return the whole fragment, delete the the row */
          DELETE FROM ATBI.DBO.i13 WHERE CURRENT OF I_cursor
        END
      END
    END
    CLOSE I_cursor
    COMMIT TRAN id_tran /* releases holdlock table lock */
    DEALLOCATE I_cursor
  END
END 
GO

based on answer below, have implemented:

DECLARE @id INT, @num_ids INT;  
EXEC [dbo].[i13_get_ids] 2, 10, @id OUTPUT, @num_ids OUTPUT;
INSERT dbo.TBL_Locations(objectid, X_Coord, Y_Coord, Loc_name)
SELECT N, 296563, 3961644, 'new test'    
FROM dbo.oidNumbers WHERE n >= @id AND n < @id + @num_ids;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T20:23:03+00:00Added an answer on May 29, 2026 at 8:23 pm

    First, you should have a numbers table (some background that I’ve written here and here). I’m picking 100,000 rows but you may need more or less depending on how much you’re going to use it.

    CREATE TABLE dbo.Numbers(n INT PRIMARY KEY);
    
    INSERT dbo.Numbers(n) 
        SELECT TOP (100000) n = ROW_NUMBER()
         OVER (ORDER BY s1.[object_id])
    FROM sys.all_objects AS s1
    CROSS JOIN sys.objects AS s2;
    

    Now, you should be able to simply say (I’m just picking 10 arbitrarily):

    DECLARE @id INT, @num_ids INT;
    
    EXEC [dbo].[i13_get_ids] 2, 10, @id OUTPUT, @num_ids OUTPUT;
    
    --INSERT dbo.TBL_Locations(objectid, X_Coord, Y_Coord, Loc_name)
    SELECT [GUID], 296563, 3961644, 'new test' + CONVERT(VARCHAR(12), n)
        FROM dbo.Numbers
        WHERE n >= @id AND n < @id + @num_ids;
    

    When you believe it’s returning the right set, uncomment the --INSERT line. Just don’t go too crazy because you are exhausting their supply of ids.


    Sorry, the stuff below this line was written with the assumption you were using unique identifiers, as your question originally stated, not integers. Leaving it in case it is useful to others.

    If you create a stored procedure that can generate multiple GUIDs, imagine this procedure that returns a set of GUIDs:

    CREATE PROCEDURE dbo.i13_get_multi_ids
    AS
    BEGIN
        SET NOCOUNT ON;
    
        -- application management magic here
    
        SELECT [GUID] = GuidValues FROM somewhere;
    END
    GO
    

    Then you could do:

    CREATE TABLE #f([GUID] UNIQUEIDENTIFIER);
    
    INSERT #f EXEC dbo.i13_get_multi_ids;
    
    INSERT dbo.TBL_LOCATIONS(objectid, X_Coord, Y_Coord, Loc_name)
    SELECT [GUID], 296563, 3961644, 
       'new test' + CONVERT(VARCHAR(12), ROW_NUMBER() OVER (ORDER BY [GUID])) 
    FROM #f ORDER BY [GUID];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following: declare @a table ( pkid int, value int ) declare @b
Given the following code snippet: type MyIntf = interface ['{C6184693-663E-419F-B2DA-4DA1A0E33417}'] procedure Foo; end; InvisiblePropInterfaces
The following query is given in a PL/SQL procedure . SELECT e.data FROM extra
A stored procedure is given that can't be changed. It returns a cursor with
So I'm trying to call an Oracle stored procedure from my C# .NET application.
Iam using the following stored procedure for select all child units of the given
Given following Ruby statements: (Read input and store each word in array removing spaces
// given following array: $data = array( 0=>array( data=>object1, col=>array( 0=>array( data=>object2, col=>array( 0=>array(
Given the following XML: <current> <login_name>jd</login_name> </current> <people> <person> <first>John</first> <last>Doe</last> <login_name>jd</login_name> </preson> <person>
Given the following: List<List<Option>> optionLists; what would be a quick way to determine the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.