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

  • Home
  • SEARCH
  • 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 9099615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:39:42+00:00 2026-06-17T00:39:42+00:00

I have this TSQL code that checks if the ‘sadsadsad’ exists and if not

  • 0

I have this TSQL code that checks if the ‘sadsadsad’ exists and if not inserts it into the table.

if not exists(select id from [ua_subset_composite] where ua = 'sadsadsad')
  begin
    insert into [ua_subset_composite]
    select 'sadsadsad',1,null,null,null,null
  end

My concern is that in production where there will be multiple threads running concurrently, a situation may occur that a record will slip thru between the not exists select and the insert.

I don’t want to add a unique constraint on the column and wondering if I can improve this SQL code so that it will guarantee the uniqueness

  • 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-17T00:39:43+00:00Added an answer on June 17, 2026 at 12:39 am

    This is what I have ended up doing

    insert into [ua_subset_composite]  WITH (TABLOCKX) (ua, os)
    select @r, 1 
    where not exists (select 1 from [ua_subset_composite] nolock where ua = @r
    

    To test the code I ran this code concurrently from multiple windows

    declare @r nvarchar(30);
    while(1=1)
    begin
    
    set @r =  convert(nvarchar(30),getdate(),21 )
    insert into [ua_subset_test]  WITH (TABLOCKX) (ua, os)
    select @r, 1 
    where not exists (select 1 from [ua_subset_test] nolock where ua = @r
    
    )
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have ' inherited ' a brilliant piece of TSQL code that does this:
I have a section of T-SQL code that looks something like this: select @automation_rate
I'm not good at t-sql, so I need help. I have this code I
I have some code that disables a trigger on a table, runs an update,
I have a tsql snippet similar to this: select p.Make, count(p.Make) as _count from
I have a database that essentially looks like this: id uniqueidentifier NOT NULL data
I am using SQL Server with t-Sql I have the following code that checks
I have this Table structure: Id int not null --PK Title varchar(50) ParentId int
Assuming that I have the following T-SQL code: SELECT * FROM Foo f INNER
Take the tsql query below: DECLARE @table TABLE(data VARCHAR(20)) INSERT INTO @table VALUES ('not

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.