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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:18:11+00:00 2026-05-11T16:18:11+00:00

I have a primary key that I don’t want to auto increment (for various

  • 0

I have a primary key that I don’t want to auto increment (for various reasons) and so I’m looking for a way to simply increment that field when I INSERT. By simply, I mean without stored procedures and without triggers, so just a series of SQL commands (preferably one command).

Here is what I have tried thus far:

BEGIN TRAN

INSERT INTO Table1(id, data_field)
VALUES ( (SELECT (MAX(id) + 1) FROM Table1), '[blob of data]');

COMMIT TRAN;

* Data abstracted to use generic names and identifiers

However, when executed, the command errors, saying that

“Subqueries are not allowed in this
context. only scalar expressions are
allowed”

So, how can I do this/what am I doing wrong?


EDIT: Since it was pointed out as a consideration, the table to be inserted into is guaranteed to have at least 1 row already.

  • 1 1 Answer
  • 3 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-11T16:18:11+00:00Added an answer on May 11, 2026 at 4:18 pm

    You understand that you will have collisions right?

    you need to do something like this and this might cause deadlocks so be very sure what you are trying to accomplish here

    DECLARE @id int
    BEGIN TRAN
    
        SELECT @id = MAX(id) + 1 FROM Table1 WITH (UPDLOCK, HOLDLOCK)
        INSERT INTO Table1(id, data_field)
        VALUES (@id ,'[blob of data]')
    COMMIT TRAN
    

    To explain the collision thing, I have provided some code

    first create this table and insert one row

    CREATE TABLE Table1(id int primary key not null, data_field char(100))
    GO
    Insert Table1 values(1,'[blob of data]')
    Go
    

    Now open up two query windows and run this at the same time

    declare @i int
    set @i =1
    while @i < 10000
    begin
    BEGIN TRAN
    
    INSERT INTO Table1(id, data_field)
    SELECT MAX(id) + 1, '[blob of data]' FROM Table1
    
    COMMIT TRAN;
    set @i =@i + 1
    end
    

    You will see a bunch of these

    Server: Msg 2627, Level 14, State 1, Line 7
    Violation of PRIMARY KEY constraint ‘PK__Table1__3213E83F2962141D’. Cannot insert duplicate key in object ‘dbo.Table1’.
    The statement has been terminated.

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

Sidebar

Related Questions

I have a table with a primary key that is auto increment. I want
I have a table that has a primary key that's an INT... I have
I have a table containing primary key and foreign key that references same table.
If you have a table with a compound primary key that is composed of
I have two tables, the first has a primary key that is an identity,
I have a process that needs the Primary Key ID of a newly added
I have found that how to determine what columns are primary key column of
I have an inline TVF that accept a primary key of a table and
I have table A in Oracle that has a primary key (id). I need
I have table with 4 primary key fields. I load that in to drop

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.