Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL Server-日期,大于和小于_Sql_Sql Server_Sql Server 2008 - Fatal编程技术网

SQL Server-日期,大于和小于

SQL Server-日期,大于和小于,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,我有一个存储过程: create procedure [dbo].[GetCarsByDates] (@startDate date, @returnDate date) as SELECT ManufacturerName, ModelName, CreationYear, Gear, CurrentKM, Picture, DATEDIFF(D, @startDate, @returnDa

我有一个存储过程:

create procedure [dbo].[GetCarsByDates]
       (@startDate date, @returnDate date) 
as
    SELECT
        ManufacturerName, ModelName,
        CreationYear,
        Gear, CurrentKM,
        Picture,
        DATEDIFF(D, @startDate, @returnDate) * PricePerDay AS [Totalprice],
        PricePerDay, PricePerDayDelayed,
        InventoryCars.LicensePlate
    FROM 
        Models
    JOIN 
        Manufacturers ON Models.ManufacturerID = Manufacturers.ManufacturerID
    JOIN 
        InventoryCars ON InventoryCars.ModelID = Models.ModelID
    JOIN 
        CarsForRent ON CarsForRent.LicensePlate = InventoryCars.LicensePlate
    WHERE 
        CarsForRent.RentalStartDate < @startDate
        AND CarsForRent.RentalReturnDate < @returnDate
        AND CarsForRent.RentalReturnDate < @startDate
    ORDER BY 
        ManufacturerName, ModelName
创建过程[dbo]。[GetCarsByDates]
(@startDate日期,@returnDate日期)
作为
挑选
制造商名称、型号名称、,
创造性思维,
档位,当前公里数,
图片,
DATEDIFF(D,@startDate,@returnDate)*每日价格为[总价],
PricePerDay,PricePerDay延迟,
车辆目录牌
从…起
模型
参加
模型上的制造商。ManufacturerID=制造商。ManufacturerID
参加
InventoryCars.ModelID=Models.ModelID上的InventoryCars
参加
CarsForRent ON CarsForRent.LicensePlate=库存车.LicensePlate
哪里
CarsCurrent.RentalStartDate<@startDate
和CarsCurrent.RentalReturnDate<@returnDate
和CarsCurrent.RentalReturnDate<@startDate
订购人
制造商名称,型号名称
我希望能够选择开始和返回日期的属性。用户输入的开始日期必须大于返回日期,这正是我所做的,但它仍然不能正常工作

问题是我得到了不可用项的行结果


我的where子句有什么问题?

我觉得您的查询应该像下面这样写。 我假设您需要查询从
startDate
returnDate
的所有可用车辆,并需要根据
CarsForRent
表的列进行检查
CarsForRent.RentalStartDate
CarsForRent.RentalReturnDate

create procedure [dbo].[GetCarsByDates]@startDate date, @returnDate date
 as
BEGIN
    select DISTINCT ManufacturerName, ModelName, 
           CreationYear,Gear, CurrentKM, 
            Picture,
            DATEDIFF(D, @startDate, @returnDate)*PricePerDay as[Totalprice],
            PricePerDay,PricePerDayDelayed, InventoryCars.LicensePlate
     from Models 
         join Manufacturers  on Models.ManufacturerID=Manufacturers.ManufacturerID
         join InventoryCars  on InventoryCars.ModelID=Models.ModelID
         join CarsForRent    on CarsForRent.LicensePlate=InventoryCars.LicensePlate

 where 
   @startDate > CarsForRent.RentalReturnDate AND 
   CarsForRent.RentalReturnDate >CarsForRent.RentalStartDate 
   AND @startDate<=@returnDate
     order by ManufacturerName, ModelName  
 END
我已经制作了一个提琴样本 请在左侧添加值并使用用例

如果希望用户仅将startdate设置为大于returndate的值,可以在实际查询之前使用if块执行此操作。例如:

create procedure [dbo].[GetCarsByDates](@startDate date, @returnDate date) 
as
if @startDate <= @returnDate OR @startDate IS NULL OR @returnDate IS NULL
  BEGIN
   /* maybe do some stuff here */
   RAISERROR 'Errormessage', 11,1´;
   RETURN;
  END

/* here Comes your query without having to check the correctnes of the input-values */
创建过程[dbo]。[GetCarsByDates](@startDate日期,@returnDate日期)
作为

如果@startDate你需要像我从你的问题中理解的那样做 where (@startDate > @returnDate) and RentalReturnDate < @startDate
哪里 (@startDate>@returnDate)和RentalReturnDate<@startDate

这是刚刚投票表决的结果,这让我想知道,SQL Server的哪个版本是语法之间的版本? where (@startDate > @returnDate) and RentalReturnDate < @startDate