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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:13:28+00:00 2026-05-11T02:13:28+00:00

I’ve a table [File] that has the following schema CREATE TABLE [dbo].[File] ( [FileID]

  • 0

I’ve a table [File] that has the following schema

CREATE TABLE [dbo].[File] (     [FileID] [int] IDENTITY(1,1) NOT NULL,     [Name] [varchar](256) NOT NULL,  CONSTRAINT [PK_File] PRIMARY KEY CLUSTERED  (     [FileID] ASC )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY] ) ON [PRIMARY] 

The idea is that the FileID is used as the key for the table and the Name is the fully qualified path that represents a file.

What I’ve been trying to do is create a Stored Procedure that will check to see if the Name is already in use if so then use that record else create a new record.

But when I stress test the code with many threads executing the stored procedure at once I get different errors.

This version of the code will create a deadlock and throw a deadlock exception on the client.

CREATE PROCEDURE [dbo].[File_Create]     @Name varchar(256) AS     SET TRANSACTION ISOLATION LEVEL SERIALIZABLE     BEGIN TRANSACTION xact_File_Create     SET XACT_ABORT ON      SET NOCOUNT ON      DECLARE @FileID int     SELECT @FileID = [FileID] FROM [dbo].[File] WHERE [Name] = @Name     IF @@ROWCOUNT=0     BEGIN         INSERT INTO [dbo].[File]([Name])         VALUES (@Name)         SELECT @FileID = [FileID] FROM [dbo].[File] WHERE [Name] = @Name     END      SELECT * FROM [dbo].[File]     WHERE [FileID] = @FileID      COMMIT TRANSACTION xact_File_Create GO 

This version of the code I end up getting rows with the same data in the Name column.

CREATE PROCEDURE [dbo].[File_Create]     @Name varchar(256) AS     BEGIN TRANSACTION xact_File_Create      SET NOCOUNT ON      DECLARE @FileID int     SELECT @FileID = [FileID] FROM [dbo].[File] WHERE [Name] = @Name     IF @@ROWCOUNT=0     BEGIN         INSERT INTO [dbo].[File]([Name])         VALUES (@Name)         SELECT @FileID = [FileID] FROM [dbo].[File] WHERE [Name] = @Name     END      SELECT * FROM [dbo].[File]     WHERE [FileID] = @FileID      COMMIT TRANSACTION xact_File_Create GO 

I’m wondering what the right way to do this type of action is? In general this is a pattern I’d like to use where the column data is unique in either a single column or multiple columns and another column is used as the key.

Thanks

  • 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. 2026-05-11T02:13:29+00:00Added an answer on May 11, 2026 at 2:13 am

    If you are searching heavily on the Name field, you will probably want it indexed (as unique, and maybe even clustered if this is the primary search field). As you don’t use the @FileID from the first select, I would just select count(*) from file where Name = @Name and see if it is greater than zero (this will prevent SQL from retaining any locks on the table from the search phase, as no columns are selected).

    You are on the right course with the SERIALIZABLE level, as your action will impact subsequent queries success or failure with the Name being present. The reason the version without that set causes duplicates is that two selects ran concurrently and found there was no record, so both went ahead with the inserts (which creates the duplicate).

    The deadlock with the prior version is most likely due to the lack of an index making the search process take a long time. When you load the server down in a SERIALIZABLE transaction, everything else will have to wait for the operation to complete. The index should make the operation fast, but only testing will indicate if it is fast enough. Note that you can respond to the failed transaction by resubmitting: in real world situations hopefully the load will be transient.

    EDIT: By making your table indexed, but not using SERIALIZABLE, you end up with three cases:

    • Name is found, ID is captured and used. Common
    • Name is not found, inserts as expected. Common
    • Name is not found, insert fails because another exact match was posted within milliseconds of the first. Very Rare

    I would expect this last case to be truly exceptional, so using an exception to capture this very rare case would be preferable to engaging SERIALIZABLE, which has serious performance consequences.

    If you do really have an expectation that it will be common to have posts within milliseconds of one another of the same new name, then use a SERIALIZABLE transaction in conjunction with the index. It will be slower in the general case, but faster when these posts are found.

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

Sidebar

Ask A Question

Stats

  • Questions 82k
  • Answers 82k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer @implementation MyObject // Posts a MyNotification message whenever called -… May 11, 2026 at 4:44 pm
  • Editorial Team
    Editorial Team added an answer I just had to set the interval of the axis… May 11, 2026 at 4:44 pm
  • Editorial Team
    Editorial Team added an answer We generally use a UDF to split a string into… May 11, 2026 at 4:44 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.