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 8881785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:24:54+00:00 2026-06-14T20:24:54+00:00

I have a stored procedure that returns an unique Id. I need to call

  • 0

I have a stored procedure that returns an unique Id. I need to call this sp to get the unique ID for each row. I must use this SP because an application also uses this.

How can I select for each row a ID that is returned from the SP?

CREATE procedure [dbo].[SelectNextNumber]

@TableName nvarchar(255)
as
begin

  declare @NewSeqVal int

  set NOCOUNT ON

  update Number --This is a table that holds for each table the max ID

  set @NewSeqVal = Next = Next + Increase

  where TableNaam= @TableName

  if @@rowcount = 0 
  begin
        Insert into Number VALUES (@TableName, 1, 1) 
        return 1
  end

  return @NewSeqVal

The number table:

CREATE TABLE [dbo].[Number](
    [TableName] [varchar](25) NOT NULL,
    [Next] [int] NULL,
    [Increase] [int] NULL

I have seen that a While loop is usable for this but in my situation I don’t know how to use a while loop.

  • 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-06-14T20:24:55+00:00Added an answer on June 14, 2026 at 8:24 pm

    Taking the singular INSERT INTO.. SELECT apart:

    Temporarily store the SELECT results away

     declare @rc int, @NewSeqVal int;
     SELECT ..
       INTO #tmp -- add this
       FROM ..
    

    Store the rowcount and get that many numbers

     set @rc = @@rowcount;
    

    For which you have to use the code in the SP directly:

     update Number --This is a table that holds for each table the max ID
     set @NewSeqVal = Next = Next + @rc
     where TableNaam= 'sometbl';
    

    Finally, the insert

     INSERT ... 
     SELECT ID = @NewSeqVal + 1 - row_number() over (ORDER BY col1)
           , {all the other columns}
     FROM #tmp;
    

    ORDER by Col1 is arbitrary, choose something sensible, or make it ORDER BY NEWID() if you don’t care.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this stored procedure that I want to use for my SAP crystal
I have a stored procedure that returns multiple resultsets, it looks something like this
We have a stored procedure that returns DB encrypted columns decrypted via DecryptByKeyAutoCert(). This
I have a stored procedure that returns hierarchical data in a single long row,
I have a stored procedure that returns the temporary table data. because i have
I have stored procedure that returns results sorted dynamically. The parent folder (this is
I have a stored procedure that returns a valueI call it from other stored
I have a stored procedure that returns data in the format of: Date |
I have a stored procedure that returns a result with 250!!! columns. But I
I have a MYSQL stored procedure SP1() that returns a result set. I want

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.