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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:24:08+00:00 2026-05-26T20:24:08+00:00

I have a table tbluser with 2 fields: userid = integer (autoincrement) user =

  • 0

I have a table “tbluser” with 2 fields:

  • userid = integer (autoincrement)
  • user = nvarchar(100)

I have a multithreaded/multi server application that uses this table.

I want to accomplish the following:

  • Guarantee that field user is unique in my table
  • Guarantee that combination userid/user is unique in each server’s memory

I have the following stored procedure:

CREATE PROCEDURE uniqueuser @user nvarchar(100) AS
BEGIN
BEGIN TRAN
    DECLARE @userID int
    SET  nocount ON
    SET  @userID = (SELECT @userID FROM tbluser WITH (TABLOCKX) WHERE  [user] = @user)
    IF @userID <> ''
    BEGIN
        SELECT  userID = @userID
    END
    ELSE
    BEGIN
        INSERT INTO  tbluser([user]) VALUES (@user)
        SELECT userID = SCOPE_IDENTITY() 
    END 
COMMIT TRAN
END

Basically the application calls the stored procedure and provides a username as parameter.
The stored procedure either gets the userid or insert the user if it is a new user.

Am I correct to assume that the table is locked (only one server can insert/query)?

  • 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-26T20:24:08+00:00Added an answer on May 26, 2026 at 8:24 pm

    If you want to guarantee that the user is unique, the only way is to a unique constraint

    ALTER  TABLE tbluser WITH CHECK
       ADD CONSTRAINT UQ_tbluser_user UQNIUE (user);
    

    Do not “roll your own” unique checks: it will fail.

    The cached data in the server’s memory conforms to the same constraint

    I’d do this. Look for user first, if not found insert, handle unique error just in case. And I’d use an OUTPUT parameter

    CREATE PROCEDURE uniqueuser
      @user nvarchar(100)
      -- ,@userid int = NULL OUTPUT
    AS
    SET NOCOUNT, XACT_ABORT ON;
    DECLARE @userID int;
    
    BEGIN TRY
        SELECT @userID FROM tbluser WHERE [user] = @user;
        IF @userID IS NULL
        BEGIN
            INSERT INTO  tbluser([user]) VALUES (@user);
            SELECT userID = SCOPE_IDENTITY() ;
        END
    END TRY
    BEGIN CATCH
        -- test for a concurrent call that just inserted before we did
        IF ERROR_NUMBER() = 2627
            SELECT @userID FROM tbluser WHERE [user] = @user;
        ELSE
           -- do some error handling
    END CATCH
    -- I prefer output parameter for this SELECT @userID AS UserID
    GO
    

    Edit: why TABLOCKX fails…

    • You only lock the table for the duration of the SELECT.
    • A 2nd process running concurrently will starting reading the table after the lock is released by process 1
    • Both processes can have @userID IS NULL because process 1 has not yet executed the INSERT
    • Process 2 gets an error when it INSERTS

    This happens because TABLOCKX modifies lock isolation and granularity, not duration.

    Edit 2: for SQL Server 2000

    CREATE PROCEDURE uniqueuser
       @user nvarchar(100)
       -- ,@userid int = NULL OUTPUT
    AS
    SET NOCOUNT, XACT_ABORT ON;
    DECLARE @userID int;
    
    SELECT @userID FROM tbluser WHERE [user] = @user;
    IF @userID IS NULL
    BEGIN
        INSERT INTO  tbluser([user]) VALUES (@user);
        IF @@ERROR = 2627
            SELECT @userID FROM tbluser WHERE [user] = @user;
        ELSE
            RAISERROR ('the fan needs cleaning', 16, 1);   
        SELECT userID = SCOPE_IDENTITY();
    END
    GO
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that contains intervals: CREATE TABLE tbl ( user_id: INTEGER, start:
I have table A in Oracle that has a primary key (id). I need
I have table that I insert data with following query (from c# code): INSERT
Say I have 5 tables, tblBlogs tblBlogPosts tblBlogPostComment tblUser tblBlogMember BlogId BlogPostsId BlogPostCommentId UserId
So I have three tables: CREATE TABLE tblUser ( [pkUserID] [int] IDENTITY(1,1) NOT NULL,
I have a table tblUser: tblUser and a table tblBCE and data in the
I need a query that will have the date, a user's name, and one
I have table of user actions, each having a user associated, a type, and
I have created one stored procedure which runs on 5000 users in tbluser table
I have table definition like below: Place (id, name) Review (id, userid, placeid) Favorite

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.