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 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 inherited a 1000-line stored procedure which produces XML by using FOR XML
A simple question: I have a Model-View-Controller setup, with Models accessing a SQL database.
This question might be kind of elementary, but here goes: I have a SQL
I have a similar question to this one SQL products/productsales I want to do
This is sort of SQL newbie question, I think, but here goes. I have
I know there have been questions in the past about SQL 2005 versus Lucene.NET
Possible Duplicate: Split string in SQL I have seen a couple of questions related
The responses I've got to this question have solved the problem I had in
Question I have an application written in Java. It is designed to run on
Question: I have a question that is apparently not answered by this already-asked Bash

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.