因此,目前我正在开发一个应用程序,需要保存具有开始日期和结束日期的值。其中一个限制是,如果我插入一个新的寄存器,我必须验证没有寄存器包含新值定义的任何月份(我知道我在这里没有正确地解释自己,但英语不是我的母语)。让我给一个可视化的例子。
让我们假设有一个表包含这些值
ID |Start Date | Ending date
001 05/08/2014 05/12/2014
002 03/05/2014 03/04/2014
所以验证必须确保如果我尝试添加像这样的新值:
003| 05/09/2014 |05/11/2014
它不允许我保存数据,因为已经有一个包含相同月份的范围,我不想添加。
我知道如何制作c#部分。然而,我正在努力与SQL,因为不是我最好的领域。任何帮助或指导将不胜感激。
假设你的表名是:Your_table_name_here,你可以试试这个脚本。
Create Table Your_table_name_here
(
Id Varchar2(25) Not Null,
Start_date Date Not Null,
Ending_date Date Not Null,
Constraint Your_table_name_here_pk Primary Key(Id)
)
/
-- create a database trigger to automatically validate the date entered --
Create Or Replace Trigger Tbi_your_table_name_here
Before Insert
On Your_table_name_here
Referencing New As New Old As Old
For Each Row
Declare
Tmpvar Number;
Begin
Tmpvar := 0;
Begin
Select 1
Into Tmpvar
From Your_table_name_here
Where (:new.Start_date Between Start_date And Ending_date
Or :new.Ending_date Between Start_date And Ending_date)
And Rownum < 2;
Raise_application_error( -20996, 'Date Range, already in database');
Exception
When No_data_found Then
Null; -- is OK, no dates found in range
When Others Then
Raise_application_error( -20997, 'Incorrect Date : ' || Sqlerrm);
End;
Exception
When Others Then
-- Consider logging the error and then re-raise
Raise;
End Tbi_your_table_name_here;
/
Show Errors
/
基本上,您创建一个触发器来验证输入到表中的数据。
希望能有所帮助