我有包含3列的表:某些事件和事件ID的开始和结束日期。将唯一时间跨度插入表
我需要插入新行并以某种方式验证开始和结束日期之间的时间跨度不会与此事件的其他时间跨度相交。
例如我有两个事件时间跨度:从1.08.2016到9.08.2016和从10.08.2016到17.08.2016。因此,我无法插入任何开始和结束日期介于1.08.2016和17.08.2016之间的新行
如何使用JavaScript,C#或T-SQL进行此验证?
我有包含3列的表:某些事件和事件ID的开始和结束日期。将唯一时间跨度插入表
我需要插入新行并以某种方式验证开始和结束日期之间的时间跨度不会与此事件的其他时间跨度相交。
例如我有两个事件时间跨度:从1.08.2016到9.08.2016和从10.08.2016到17.08.2016。因此,我无法插入任何开始和结束日期介于1.08.2016和17.08.2016之间的新行
如何使用JavaScript,C#或T-SQL进行此验证?
DECLARE @DataX TABLE (
EventID INT IDENTITY
,StartDate DATE
,EndDate DATE
)
INSERT INTO @DataX VALUES
('2016-08-01','2016-08-09')
,('2016-08-10','2016-08-17')
DECLARE @NewStart DATE = '2016-08-05'
,@NewEnd DATE = '2016-08-18'
,@NewStartValid TINYINT
,@NewEndValid TINYINT
;WITH Validate
AS (
SELECT SUM(CASE WHEN @NewStart BETWEEN StartDate AND EndDate THEN 1 ELSE 0 END) AS VStart,
SUM(CASE WHEN @NewEnd BETWEEN StartDate AND EndDate THEN 1 ELSE 0 END) AS VEnd
FROM @DataX
)
SELECT @NewStartValid = VStart, @NewEndValid = VEnd
FROM Validate
SELECT @NewStartValid, @NewEndValid
有效是0,无效是> 0本实施例表明在开始日期是日期范围之一内(结果:1)和结束日期不是(结果:0)。
这是我对C#的解决方案。用下面的控制做到这一点,你需要一个表格(窗口):
X1“的DataGridView”(表格看与数据的列)
X1“的NumericUpDown”(要选择事件ID)
X2 “的DateTimePicker”(该选择起始和结束日期)
X1 “按钮”(为事件添加到表)
这里有代码:
// This is your table with data in three columns
DataTable events = new DataTable();
// When the program starts
public Form1()
{
InitializeComponent();
// Add the columns to the table
events.Columns.Add("ID", typeof(int));
events.Columns.Add("Start Date", typeof(DateTime));
events.Columns.Add("End Date", typeof(DateTime));
// Set data and formats
dataGridView1.DataSource = events;
dataGridView1.Columns[1].DefaultCellStyle.Format = "dd/MM/yyyy"; // The way the date is shown
dataGridView1.Columns[2].DefaultCellStyle.Format = "dd/MM/yyyy"; // Ex. 27-07-2016
}
// Add The row containing information about the event
private void btnAdd_Click(object sender, EventArgs e)
{
// Weather or not the row should be added
bool addRow = true;
// Skip this if no rows are added yet
foreach (DataRow row in events.Rows)
{
// Row id and dates
int eventId = (int)row[0];
DateTime rowStartDate = (DateTime)row[1];
DateTime rowEndDate = (DateTime)row[2];
// If new start date lies inside the timespan of an existing event
bool newStartDateBetween = rowStartDate.Date <= dateTimePicker1.Value.Date && rowEndDate.Date >= dateTimePicker1.Value.Date;
// If new end date lies inside the timespan of an existing event
bool newEndDateBetween = rowStartDate.Date <= dateTimePicker2.Value.Date && rowEndDate.Date >= dateTimePicker2.Value.Date;
// If any of these statements are true, show the error and dont add the new row
if (eventId == numericUpDown1.Value || newStartDateBetween || newEndDateBetween || dateTimePicker2.Value.Date < dateTimePicker1.Value.Date)
{
if (eventId == numericUpDown1.Value)
MessageBox.Show("Event ID already taken!");
if (newStartDateBetween)
MessageBox.Show("The starting date is within the timespan of event ID: " + row[0] + "!");
if (newEndDateBetween)
MessageBox.Show("The ending date is within the timespan of event ID: " + row[0] + "!");
if (dateTimePicker2.Value.Date < dateTimePicker1.Value.Date)
MessageBox.Show("The ending date is before the starting date!");
// Dont add the row
addRow = false;
break;
}
}
// Add the row if no errors
if (addRow)
events.Rows.Add((int)numericUpDown1.Value, dateTimePicker1.Value, dateTimePicker2.Value);
}
希望这有助于。
不幸的是,它不会帮助,因为我正在使用bpm'online(如果你知道它是什么) –
那么你可以应用逻辑吗? – MasterXD
明天我会尝试你所有的想法,希望其中的一个人会帮助你 –
下面将返回任何conficts
Declare @Table table (EventID int,StartDate Date,EndDate Date)
Insert into @Table values
(1,'2016-08-01','2016-08-09'),
(2,'2016-08-10','2016-08-17')
Declare @StartDate Date = '2016-08-05'
Declare @EndDate Date = '2016-08-18'
Select *
,ValidStart = IIF(@StartDate Between StartDate and EndDate,0,1)
,ValidEnd = IIF(@EndDate Between StartDate and EndDate,0,1)
From @Table
Where @StartDate Between StartDate and EndDate
or @EndDate Between StartDate and EndDate
返回
EventID StartDate EndDate ValidStart ValidEnd
1 2016-08-01 2016-08-09 0 1
沿着从你的榜样之后,假设你的表有列'id','start_date'和'end_date',这将是值得检查是否要插入的start_date值 max(end_date列)?假设你所有的事件都是连续的。如果它们不是连续的,则需要遍历所有事件行。除非我错过了应该做的事情? –
sigint
不幸的是,可能会有差距,如1.08.16-9.08.16和13.08.16-15.08.16 –