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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:20:33+00:00 2026-06-01T18:20:33+00:00

I have a table that contains two columns ID | Name —————- 1 |

  • 0

I have a table that contains two columns

ID | Name
----------------
1  | John
2  | Sam
3  | Peter
6  | Mike

It has missed IDs. In this case these are 4 and 5.

How do I find and insert them together with random names into this table?

Update: cursors and temp tables are not allowed. The random name should be ‘Name_’+ some random number. Maybe it would be the specified value like ‘Abby’. So it doesn’t matter.

  • 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-01T18:20:35+00:00Added an answer on June 1, 2026 at 6:20 pm

    Using a recursive CTE you can determine the missing IDs as follows

    DECLARE @Table TABLE(
            ID INT,
            Name VARCHAR(10)
    )
    
    INSERT INTO @Table VALUES (1, 'John'),(2, 'Sam'),(3,'Peter'),(6, 'Mike')
    
    DECLARE @StartID INT,
            @EndID INT
    
    SELECT  @StartID = MIN(ID),
            @EndID = MAX(ID)
    FROM    @Table
    
    ;WITH IDS AS (
            SELECT  @StartID IDEntry
            UNION ALL
            SELECT  IDEntry + 1
            FROM    IDS
            WHERE   IDEntry + 1 <= @EndID
    )
    SELECT  IDS.IDEntry [ID]
    FROM    IDS LEFT JOIN
            @Table t    ON  IDS.IDEntry = t.ID
    WHERE   t.ID IS NULL
    OPTION (MAXRECURSION 0)
    

    The option MAXRECURSION 0 will allow the code to avoid the recursion limit of SQL SERVER

    From Query Hints and WITH common_table_expression (Transact-SQL)

    MAXRECURSION number Specifies the maximum number of recursions
    allowed for this query. number is a nonnegative integer between 0 and
    32767. When 0 is specified, no limit is applied. If this option is not specified, the default limit for the server is 100.

    When the specified or default number for MAXRECURSION limit is reached
    during query execution, the query is ended and an error is returned.

    Because of this error, all effects of the statement are rolled back.
    If the statement is a SELECT statement, partial results or no results
    may be returned. Any partial results returned may not include all rows
    on recursion levels beyond the specified maximum recursion level.

    Generating the RANDOM names will largly be affected by the requirements of such a name, and the column type of such a name. What exactly does this random name entail?

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

Sidebar

Related Questions

Should a database table that contains two columns that are foreign keys have a
I have a table with two columns - the key contains the image name,
I have two tables that contain completely separate information: table: tires columns: Tire_id name
I have two tables: a schedule table that contains information about how an employee
I have two simple tables in my database. A card table that contains Id,
I have a table that contains a column of XML Datatype (column name FileContent).
I have a database with a table that contains Name and CompanyName . They
I have two tables one named Person , which contains columns ID and Name
I have two tables, one that contains volunteers, and one that contains venues. Volunteers
I have two functions to add and remove table rows that contain a form

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.