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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:53:08+00:00 2026-05-21T23:53:08+00:00

My question is: Have an sql stored procedure which brings me a list of

  • 0

My question is:

Have an sql stored procedure which brings me a list of free rooms between two date in an hotel_reservation table for a given hotelid and bookin ,bookout dates.

Here is the procedure :

    ALTER PROCEDURE dbo.StoredProcedure1
    (
    @DateStart date,
    @DateEnd date,
    @hotel_id int
    )

AS
    SELECT DISTINCT room_type_id, room_id
    FROM Room
WHERE room_id NOT IN (
        SELECT room_id
        FROM Hotel_Reservation
        WHERE @DateStart BETWEEN bookingStart AND bookingEnd
    OR @DateEnd BETWEEN bookingStart AND bookingEnd
    or bookingStart BETWEEN @DateStart AND @DateEnd  
    or bookingEnd BETWEEN @DateStart AND @DateEnd
    and hotel_id = @hotel_id 
) and hotel_id = @hotel_id 
    RETURN

Definition of Room table :

room_id          int
hotel_id         int
room_type_id     varchar
price            money
room_description varchar

In room table I used duplicated key with room_id and Hotel_id cause Hotel with id 201 can have room_id 101 also Hotel with id 202 can have room_id 101 to avoid from repeated columns I used this.

definition of hotel_reservation table

reservation_id int
customer_id    varchar
bookingStart   date
bookingEnd     date
status         varchar
room_id        int
hotel_id       int
date           date

The Problem is:

When I run this procedure If a user booked a room with id 101 at hotel 201, another user cant see the room with id 101 at hotel with id 202. It books all the 101 id rooms from the hotels.

How can I improve my procedure to overcome this problem?

Thanks for viewing..

Image1, Image2

  • 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-21T23:53:09+00:00Added an answer on May 21, 2026 at 11:53 pm

    Change to NOT EXSTS which allows multiple conditions?

    In this case it can check both hotel and room ids.

    SELECT DISTINCT room_type_id, room_id
    FROM Room R
    WHERE NOT EXISTS (
            SELECT *
            FROM Hotel_Reservation H
            WHERE
        (
        @DateStart BETWEEN H.bookingStart AND H.bookingEnd
        OR @DateEnd BETWEEN H.bookingStart AND H.bookingEnd
        or H.bookingStart BETWEEN @DateStart AND @DateEnd  
        or H.bookingEnd BETWEEN @DateStart AND @DateEnd
        )
        and  --here is where we check **that room for that hotel**
        H.hotel_id = R.hotel_id and H.room_id = R.room_id
    ) 
    and 
    R.hotel_id = @hotel_id
    

    Edit:

    Thank you to Andriy M for correcting my AND/OR operator precedence with some brackets

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

Sidebar

Related Questions

I have a simple question regarding T-SQL. I have a stored procedure which calls
I have a stored procedure called WEB_SEL_SECURITY_QUESTIONS in my sql database which returns a
I am developing a PL/SQL stored procedure which inserts rows into a table. The
Helo, My question is I have one Stored Procedure in SQL Server that returns
I have a stored procedure which is the first step in an SQL Job.
I have two Microsoft SQL Server 2000 databases, and a stored procedure on one
I have this question in which I have a SQL Server Compact Edition database
I have an SQL question which may be basic to some but is confusing
I have a question on Date difference in (My)Sql with c#. In my database,
I have a small sql question for you. I have two queries: SELECT tbl_CostPrevisions.Month,

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.