我有包含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 –