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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:53:38+00:00 2026-05-26T02:53:38+00:00

I have a sql statement which is a routine insert of two values into

  • 0

I have a sql statement which is a routine insert of two values into a table (values meaning fields). These are ints/nvarchars.

However, for the 3rd field of the table (which can be null), I want to insert the max number of rows in the table + 1. How can I do this?

EDIT:

Table is as such

Postcode  Active ID
NULL       1      14
  • 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-26T02:53:38+00:00Added an answer on May 26, 2026 at 2:53 am

    This code shows you how it can be done and what trouble you can get into if you need ID to be a unique value in the table. There are more situations where you might end up with duplicates. One would be that two users add rows to the table at the same time.

    declare @T table(Postcode varchar(5), Active bit, ID bigint)
    
    insert into @T (Postcode, Active, ID)
    select '1111', 1, count(*)+1
    from @T
    
    insert into @T (Postcode, Active, ID)
    select '2222', 0, count(*)+1
    from @T
    
    delete from @T where Postcode = '1111'
    
    insert into @T (Postcode, Active, ID)
    select '3333', 0, count(*)+1
    from @T
    
    select *
    from @T
    

    Result:

    Postcode Active ID
    -------- ------ --------------------
    3333     0      2
    2222     0      2
    

    If you need the values to be unique you should use an identity column instead. Perhaps even make it a primary key or at least add a unique constraint on the ID column.

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

Sidebar

Related Questions

I have SQL query statement which used to display the contents in the table.
I have this sql update statement which updates a remote table. Is there any
I have to write an SQL statement which contain a field that contain two
We have a Single Statement FUNCTION in SQL Server 2005 which uses CONTAINSTABLE(). All
I have a SQL statement that looks like: SELECT [Phone] FROM [Table] WHERE (
I have this sql statement: CREATE TABLE [dbo].[User]( [UserId] [int] IDENTITY(1,1) NOT NULL, [FirstName]
I have a sql statement which is hardcoded in an existing VB6 app. I'm
I'm have a SQL statement which I am trying to transform in a LINQ
I have the following SQL statement which returns a single record as expected: select
I have a SQL statement in which I do this ... group by date

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.