简体   繁体   中英

SQL Stored Proc For Search Query Based on Multiple Parameters

Ok, I need a little help with a stored proc, that is a little bit over my head and I am not sure how to proceed. The SP is for a search function in a project I am working on. It is to find available seats based on selections from the end user. The user should be able to search based on the following:

Departure airport (or "ANY")

Arrival airport (or "ANY")

Earliest Depart time

Latest Depart time

Min Seats Available

Max Flights to be Returned

I initially thought separate SELECT statements would do the trick, but as you probably know, the select statements were treated as separate queries and the return resulted in 6 different returns. So, I attempted to include them in one SELECT, but my logic must be screwy, because the return is faulty. If anyone can point me in the right direction it would be very much appreciated. Code follows:

ALTER PROCEDURE [dbo].[usp_FindSeats]
(
@DepartureAirport           char(3),
@ArrivalAirport             char(3),
@EarliestDepTime            datetime,
@LatestDepTime              datetime,
@minSeatsAvailable          int,
@maxFlightsRequested        int

 )

AS
BEGIN

SELECT * FROM Flight
WHERE
(@DepartureAirport = UPPER('ANY') OR DepartAirport = @DepartureAirport)
AND
(@ArrivalAirport = UPPER('ANY') OR ArriveAirport = @ArrivalAirport) 
AND
(DepartTime >= @EarliestDepTime) 
AND
(DepartTime <= @LatestDepTime) 
AND
(FlightSeatsAvailiable >= @minSeatsAvailable) 
AND
((SELECT COUNT(FlightID) FROM Flight) <= @maxFlightsRequested)


--IF (@DepartureAirport = UPPER('ANY')) 
--BEGIN
--(SELECT * FROM Flight WHERE DepartAirport != '')
--END
--ELSE
--SELECT * FROM Flight WHERE DepartAirport = @DepartureAirport


--IF (@ArrivalAirport = UPPER('ANY'))
--Begin
--(SELECT * FROM Flight WHERE ArriveAirport != '')
--END
--ELSE
--SELECT * FROM Flight WHERE ArriveAirport = @ArrivalAirport


--SELECT * FROM Flight WHERE DepartTime >= @EarliestDepTime

--SELECT * FROM Flight WHERE DepartTime <= @LatestDepTime

--SELECT * FROM Flight WHERE FlightSeatsAvailiable >= @minSeatsAvailable

--SELECT * FROM Flight WHERE (SELECT COUNT(FlightID) FROM Flight) <= @maxFlightsRequested

Simplest approach would be, if i understand your need correctly:

SELECT * FROM Flight WHERE (@DepartureAirport = UPPER('ANY') OR DepartAirport = @DepartureAirport) UNION SELECT * FROM Flight WHERE (@ArrivalAirport = UPPER('ANY') OR ArriveAirport = @ArrivalAirport) AUNION SELECT * FROM Flight WHERE (DepartTime >= @EarliestDepTime) UNION SELECT * FROM Flight WHERE (DepartTime <= @LatestDepTime) UNION SELECT * FROM Flight WHERE (FlightSeatsAvailiable >= @minSeatsAvailable) UNION SELECT * FROM Flight WHERE ((SELECT COUNT(FlightID) FROM Flight) <= @maxFlightsRequested)

this would aggregate all the results depending on user's input.

Try this:

ALTER PROCEDURE [dbo].[usp_FindSeats]
(
    @DepartureAirport           char(3),
    @ArrivalAirport             char(3),
    @EarliestDepTime            datetime,
    @LatestDepTime              datetime,
    @minSeatsAvailable          int,
    @maxFlightsRequested        int    
 )    
AS

SELECT  TOP(@maxFlightsRequested) * 
FROM    Flight
WHERE   (@DepartureAirport = 'any' OR DepartAirport = @DepartureAirport)
        AND (@ArrivalAirport = 'any' OR ArriveAirport = @ArrivalAirport) 
        AND DepartTime >= @EarliestDepTime
        AND DepartTime <= @LatestDepTime
        AND FlightSeatsAvailiable >= @minSeatsAvailable

Note that if you limit the number of rows returned, it's probably a good idea to sort the result in some way (ORDER BY).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM