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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:06:36+00:00 2026-05-24T11:06:36+00:00

Consider this simple table : table create statement is : CREATE TABLE [dbo].[Test_Serializable]( [Id]

  • 0

Consider this simple table :
enter image description here

table create statement is :

CREATE TABLE [dbo].[Test_Serializable](
[Id] [int] NOT NULL,
[Name] [nvarchar](50) NOT NULL
)

so there is not any primary key or index.

consider it’s emopty and has not any row.I want to Insert this row (1,'nima') but I want to check if there is a row with Id=1 or not.if yes call RAISERROR and if no Insert row.I write this script:

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRY
BEGIN TRAN ins

    IF EXISTS(SELECT * FROM Test_Serializable ts WITH(xlock,ROWLOCK) WHERE ts.Id=1)
        RAISERROR(N'Row Exists',16,1);


    INSERT INTO Test_Serializable
    (
        Id,
        [Name]
    )
    VALUES
    (
        1,
        'nima'
    )
   COMMIT TRAN ins
     END TRY
     BEGIN CATCH
        DECLARE @a  NVARCHAR(1000);
        SET @a=ERROR_MESSAGE();
        ROLLBACK TRAN ins
        RAISERROR(@a,16,1);
     END CATCH

this script works fine but there is interesting point.

I run this script from 2 SSMS and step by step run this 2 scripts(in debug mode).Interesting point is however my table has no row but one of the script when reach IF EXIST statement lock the table.

My question is whether (XLOCK,ROWLOCK) locks entire table because there is no row?or it locks phantom row 🙂 !!???

Edit 1)

This is my scenario:

I have a table with for example 6 fields

enter image description here

this is Uniqueness Rules:

1)City_Code + F1_Code are Unique

2)City_Code + F2_Code are Unique

3)City_Code + F3_Code + F4_Code are uinque

the problem is user may want to fill city_code and F1_Code and when it want Insert it in other fileds we must have Empty String or 0 (for numeric fields) value.

If user want to fill City_Code + F3_Code + F4_Code then F1_Code and F2_Code must have Empty String values

How I can check this better?I can’t create any Unique Index for every rules

  • 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-24T11:06:37+00:00Added an answer on May 24, 2026 at 11:06 am

    To answer your question, the SERIALIZABLE isolation level performs range locks which would include non-existant rows within the range.

    http://msdn.microsoft.com/en-us/library/ms191272.aspx

    Key-range locking ensures that the following operations are
    serializable:

    Range scan query

    Singleton fetch of nonexistent row

    Delete operation

    Insert operation

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

Sidebar

Related Questions

Consider a simple table with an auto-increment column like this: CREATE TABLE foo (
Consider this example table (assuming SQL Server 2005): create table product_bill_of_materials ( parent_product_id int
Let's consider this table: [name] [type] Ken Anderson 1 John Smith 2 Bill Anderson
Consider this code (Java, specifically): public int doSomething() { doA(); try { doB(); }
Consider the following table with a constraint that's a bit daft but simple enough
Consider this simple query: SELECT * FROM table1 JOIN table2 USING(pid) WHERE pid='2' ;
Is there an SQL injection possibility even when using mysql_real_escape_string() function? Consider this sample
Consider this sample code: <div class=containter id=ControlGroupDiv> <input onbeforeupdate=alert('bingo 0'); return false; onclick=alert('click 0');return
Consider this problem: I have a program which should fetch (let's say) 100 records
Consider this code... using System.Threading; //... Timer someWork = new Timer( delegate(object state) {

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.