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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:34:52+00:00 2026-05-30T08:34:52+00:00

I am trying to insert missing rows into a table. One of the columns

  • 0

I am trying to insert missing rows into a table. One of the columns is OrderNumber (sort number), this column should be +1 of the max value of OrderNumber returned for sID in the table. Some sIDs do not appear in the SPOL table which is why there is the WHERE clause at the end of the statement. I would run this statement again but set OrderNumber to 1 for the records where sID does not currently exist in the table.

The statement below doesn’t work due to the OrderNumber causing issues with the primary key which is sID + OrderNumber.

How can I get the OrderNumber to increase for each row that is inserted based on the sID column?

INSERT INTO SPOL(sID, OrderNumber, oID)
   SELECT           
      sID, OrderNumber, oID
   FROM
      (SELECT 
           sID, 
           (SELECT Max(OrderNumber) + 1
            FROM SPOL
            WHERE sID = TMPO.sID) AS OrderNumber, 
           oID
       FROM TMPO
       WHERE NOT EXISTS (SELECT * FROM SPOL
                         WHERE SPOL.oID = TMPO.oID)
      ) AS MyData
   WHERE
       OrderNumber IS NOT NULL  
  • 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-30T08:34:53+00:00Added an answer on May 30, 2026 at 8:34 am

    It’s much better to handle this in the database design with an identity column – you don’t mention whether or not you can change the schema but hopefully you can as queries will end up a lot cleaner if you don’t have to manage it yourself.

    You can set the Identity property to on for your OrderNumber column in SQL Server management studio, but the script it would generate clones the table with the new specification, inserts the values you’ve already got with Identity_Insert on, drops the original table, and renames the temporary one to replace it – this has massive overheads depending on how many rows you’ve got.

    The most efficient way to go about it is probably:

    1. create an additional column with the identity property on
    2. copy across the values
    3. rename the original column
    4. rename the new column to the same name as the original
    5. remove the original OrderNumber column

    Once it’s done, it’s done though – and looks after itself. Wouldn’t you rather your insert statement simply said something like this:

    INSERT INTO SPOL (sID, oID)
    SELECT sID, oID,
    FROM   TMPO
    WHERE  OrderNumber IS NOT NULL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I am trying to insert multiples rows of data from one table to
I'm trying to insert a bunch of rows into an empty UITableView in one
im trying to insert this query with mysql_query INSERT INTO um_group_rights (`um_group_id`,`cms_usecase_id`,`um_right_id`) VALUES (2,1,1)
Am trying to insert XML into XML Column.. getting following error: . Msg 6819,
I am trying to INSERT INTO a table using the input from another table.
I'm trying to insert a comment character into a string something similar to this:
I'm trying to insert a column into an existing DataSet using C#. As an
I'm trying to find duplicate rows based on mixed columns. This is an example
I'm trying to post data with AJAX and insert it into a database table.
Trying to duplicate some rows in a table but just change the ssreportid column

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.