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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:08:29+00:00 2026-05-26T12:08:29+00:00

I’m using SQL Server 2K8 and have a table used solely to generate ids

  • 0

I’m using SQL Server 2K8 and have a table used solely to generate ids so that the primary key is unique across multiple tables — the uniqueness across multiple tables is for an element hiearchy tree that requires each nodes to have a unique id regardless of type.

It only has one auto increment identity column from which I’d normally used @@IDENTITY to extract the ID if I were to insert a record one by one. However, I’m trying to optimize and do the inserts in a batch, but first need to generate a batch of IDs from this table.

CREATE TABLE [dbo].[MyTreeElementIDGenTab](
        [MyTreeElementID] [int] IDENTITY(1,1) NOT NULL
)   

Also I know I could use a while loop/cursor, but was wondering if given a temp table of row data if I could use a sql batch statement to get the IDs out of this table to set in the temp table before inserting into the actual table.

I was thinking I could use ROW_NUMBER() to generate an Index and put the corresponding Index in the initial table, but didn’t make any progress. Any help is appreciated!

DECLARE @NodeTypeATab TABLE
(
    NodeTypeATabId INT NULL,        -- To be populated by dbo.MyTreeElementIDGenTab
    Name NVARCHAR(MAX),
    ItemIndex INT NOT NULL      -- I can initially populate this from the client starting 
                            -- with Index # 1 for joining but maybe I don't need it?
)

-- Populate @NodeTypeATab with test data here

DECLARE @EntityIdTab TABLE
(   
    ElementId INT NOT NULL,
    ItemIndex INT NOT NULL
)

-- This below doesn't compile to generate a new ElementId to later be set in NodeTypeATabId above
-- I want to output the generated ID into the temp table but also have a "correlation/index ID" to set
-- back in the original table or some way if not via a correlation/index ID 

INSERT INTO dbo.MyTreeElementIDGenTab DEFAULT VALUES
OUTPUT INSERTED.MyTreeElementID, ROW_NUMBER() OVER(ORDER BY MyTreeElementID ASC) INTO @EntityIdTab
FROM @NodeTypeATab
  • 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-26T12:08:29+00:00Added an answer on May 26, 2026 at 12:08 pm

    Not sure that I understand where you want to put your generated id’s but you can use merge and a numbers table to generate multiple id’s at once. Here is some sample code that uses master..spt_values to generate 10 id’s.

    merge into MyTreeElementIDGenTab as T
    using (select Number
           from master..spt_values
           where Number between 1 and 10 and
                 [Type] = 'P') as S(I) 
    on 0=1
    when not matched then
      insert default values
    output inserted.MyTreeElementID;  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

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.