简体   繁体   English

日期不能在将来的SQL中

[英]Date cannot be in the future SQL

I'm trying to add a kind of constraint that will prevent the user from entering dates in the future, I need it to raise an error when the user tries to do so. 我正在尝试添加一种约束,该约束将阻止用户将来输入日期,当用户尝试输入日期时,我需要它来引发错误。

This is what I have so far: 这是我到目前为止的内容:

Create Procedure CustomerBooks (@month int, @year int)
AS
BEGIN
    SELECT     
       SaleNumber, 
       month(saledate) as SaleMonth, year(saledate) as SaleYear,
       CustomerNumber, EmployeeNumber, SubTotal, GST, Total
    FROM         
       sale
    Where  
       month(saledate) = @month 
       and YEAR (saledate) = @year 
End

If salemonth > GETDATE(Month(saledate))  
   or saleyear > GETDATE(YEAR(saledate))
begin
   Raiserror ('Invalid entry, you cannot enter future dates',16,1)
end


EXEC dbo.CustomerBooks @month = '1', @year = '2012'

If you are using SQL Server, the simplest solution would be to add a CHECK CONSTRAINT to prevent anyone entering a date beyond the (SQL Server) system date. 如果使用SQL Server,最简单的解决方案是添加CHECK CONSTRAINT以防止任何人输入超出(SQL Server)系统日期的日期。

ALTER TABLE Sale ADD CONSTRAINT CKC_SALE_SALEDATE CHECK (SaleDate <= GetDate());

Edit 1 Regarding OP's comment on adding a check constraint to a stored procedure 编辑1 关于OP关于向存储过程添加检查约束的评论

The benefit of a CHECK CONSTRAINT is that it can't be bypassed without disabling it. CHECK CONSTRAINT的好处在于,如果不禁用它,就不能绕过它。

There will always be instances where someone inserts/updates the data without going through the stored procedure you've set up. 在某些情况下,总是有人在不执行您设置的存储过程的情况下插入/更新数据。 The constraint will prevent entering incorrect data. 该约束将防止输入错误的数据。


Edit 2 Regarding OP's error when checking on GetDate() 编辑2 关于检查GetDate()时OP的错误

The following construct currently doesn't compile 以下构造当前无法编译

   If salemonth > GETDATE(Month(saledate))  
   or saleyear > GETDATE(YEAR(saledate))

The error message hints to what is in error here, the GetDate() function doesn't take any parameters. 错误消息提示这里有什么错误,GetDate()函数没有任何参数。 Most likely, I suspect you meant to write something like this 最有可能的是,我怀疑您是想写这样的东西

   If salemonth > MONTH(GetDate())  
   or saleyear > YEAR(GetDate())

Edit 3 编辑3

Verify that the inputs are not in the future could be done by using following if/then/else construct. 可以通过使用以下if / then / else构造来验证将来是否不需要输入。 Another option would be to convert the inputs to an actual date and check on that. 另一个选择是将输入转换为实际日期并检查该日期。

IF (YEAR(GetDate()) < @year)    
  Raiserror ('Invalid entry, you cannot enter future dates',16,1)
ELSE IF (YEAR(GetDate()) = @year) AND (MONTH(GetDate()) < @month) 
  Raiserror ('Invalid entry, you cannot enter future dates',16,1)

SQL Fiddle Example SQL小提琴示例

Use a trigger Instead. 使用触发器代替。

Create or replace trigger tri_name
before insert on  sale 
begin
If salemonth > GETDATE(Month(saledate)) or saleyear > GETDATE(YEAR(saledate))
then 
Raiserror ('Invalid entry, you cannot enter future dates',16,1)
end;
/

May be this will help You. 可能对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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