您需要显示在某个时间段内销售的产品列表(SaleDate 使用日期时间数据类型),但可能没有第二个日期,在这种情况下您只需要显示当天的产品列表参数中指定的。就我而言,如果输入一个参数,则sql查询不会返回任何内容,但如果指定两个参数,则所有内容都会成功返回。我不太明白为什么会发生这种情况。代码:
@FirstDate date,
@SecondDate date = NULL
as
if (@SecondDate IS NULL)
select Product.Title, Manufacturer.[Name], ProductSale.SaleDate, (Product.Price * ProductSale.Quantity) AS 'Total Earn' from
ProductSale join Product on Product.ID = ProductSale.ProductID join Manufacturer on Manufacturer.ID = Product.ManufacturerID
where SaleDate = @FirstDate
else if (select count(*) from ProductSale where SaleDate between @FirstDate and @SecondDate) != 0
select Product.Title, Manufacturer.[Name], ProductSale.SaleDate, (Product.Price * ProductSale.Quantity) AS 'Total Earn' from
ProductSale join Product on Product.ID = ProductSale.ProductID join Manufacturer on Manufacturer.ID = Product.ManufacturerID
where SaleDate between @FirstDate and @SecondDate
else if (select count(*) from ProductSale where SaleDate between @FirstDate and @SecondDate) = 0
print 'Товаров не найдено!'
exec PR_2DATES '16.03.2024'
| Title | Name | SaleDate | TotalEarn |