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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:48:35+00:00 2026-05-13T21:48:35+00:00

Our application needs a simple scheduling mechanism – we can schedule only one visit

  • 0

Our application needs a simple scheduling mechanism – we can schedule only one visit per room for the same time interval (but one visit can be using one or more rooms). Using SQL Server 2005, sample procedure could look like this:

CREATE PROCEDURE CreateVisit
    @start datetime, @end datetime, @roomID int
AS
BEGIN
DECLARE @isFreeRoom INT

BEGIN TRANSACTION

SELECT @isFreeRoom = COUNT(*) 
FROM visits V
INNER JOIN visits_rooms VR on VR.VisitID = V.ID
WHERE @start = start AND @end = [end] AND VR.RoomID = @roomID

IF (@isFreeRoom = 0)
BEGIN
    INSERT INTO visits (start, [end]) VALUES (@start, @end)
    INSERT INTO visits_rooms (visitID, roomID) VALUES (SCOPE_IDENTITY(), @roomID)
END

COMMIT TRANSACTION
END

In order to not have the same room scheduled for two visits at the same time, how should we handle this problem in procedure? Should we use SERIALIZABLE transaction isolation level or maybe use table hints (locks)? Which one is better?

  • 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-13T21:48:36+00:00Added an answer on May 13, 2026 at 9:48 pm

    I would have the calling application pass in a comma separated list of room IDs and split them in the SQL, inserting all rows with one INSERT. Doing that, with the proper locking hints (on a single SELECT), should allow your scheduling procedure to work.

    I prefer the number table approach to split a string in TSQL, but you can use your own split method if you have one. Here is how to make the number table split approach work:

    For this method to work, you need to do this one time table setup:

    SELECT TOP 10000 IDENTITY(int,1,1) AS Number
        INTO Numbers
        FROM sys.objects s1
        CROSS JOIN sys.objects s2
    ALTER TABLE Numbers ADD CONSTRAINT PK_Numbers PRIMARY KEY CLUSTERED (Number)
    

    Once the Numbers table is set up, create this split function:

    CREATE FUNCTION [dbo].[FN_ListToTable]
    (
         @SplitOn  char(1)      --REQUIRED, the character to split the @List string on
        ,@List     varchar(8000)--REQUIRED, the list to split apart
    )
    RETURNS TABLE
    AS
    RETURN 
    (
    
        ----------------
        --SINGLE QUERY-- --this will not return empty rows
        ----------------
        SELECT
            ListValue
            FROM (SELECT
                      LTRIM(RTRIM(SUBSTRING(List2, number+1, CHARINDEX(@SplitOn, List2, number+1)-number - 1))) AS ListValue
                      FROM (
                               SELECT @SplitOn + @List + @SplitOn AS List2
                           ) AS dt
                          INNER JOIN Numbers n ON n.Number < LEN(dt.List2)
                      WHERE SUBSTRING(List2, number, 1) = @SplitOn
                 ) dt2
            WHERE ListValue IS NOT NULL AND ListValue!=''
    
    );
    GO 
    

    You can now easily split a CSV string into a table and join on it:

    select * from dbo.FN_ListToTable(',','1,2,3,,,4,5,6777,,,')
    

    OUTPUT:

    ListValue
    -----------------------
    1
    2
    3
    4
    5
    6777
    
    (6 row(s) affected)
    

    This is what I would then make your procedure:

    CREATE PROCEDURE CreateVisit
        @start datetime, @end datetime, @roomIDs varchar(8000)
    AS
    BEGIN
    DECLARE @RowID INT
    
    BEGIN TRANSACTION
    
    IF NOT EXISTS (SELECT
                       1
                       FROM visits_rooms           (HOLDLOCK,UPDLOCK)  v 
                           INNER JOIN dbo.FN_ListToTable(',',@roomIDs) r ON v.RoomID=r.ListValue
                       WHERE @start = start AND @end = [end] AND VR.RoomID = @roomID --copy of your logic, but shouldn't it be WHERE start>=@start AND [end]<=@end
                  )
    BEGIN
        INSERT INTO visits (start, [end]) VALUES (@start, @end)
        SELECT @RowID=SCOPE_IDENTITY()
        INSERT INTO visits_rooms
                (visitID, roomID)
            SELECT 
                @RowID, r.ListValue
                FROM dbo.FN_ListToTable(',',@roomIDs) r
    
    END
    
    COMMIT TRANSACTION
    END
    

    GO

    if you have many RoomIDs one one schedule attempt, you could split them into a @TempTable variable or an actual table #TempTable first and then reuse it in the IF EXISTS and INSERT SELECT.

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

Sidebar

Ask A Question

Stats

  • Questions 391k
  • Answers 391k
  • 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 From the Sun forums (http://72.5.124.102/thread.jspa?messageID=10931926#10931926), while you cannot hide the… May 15, 2026 at 1:26 am
  • Editorial Team
    Editorial Team added an answer You could write regex constraints: routes.MapRoute( "Search", // Route name… May 15, 2026 at 1:26 am
  • Editorial Team
    Editorial Team added an answer All you should need to do is setup which configuration… May 15, 2026 at 1:26 am

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.