I've got a big problem. I have to store date times and values in my SQL Server database.
I wrote a website, which contains a table with entries.
One entry contains: the date, time and a value
For example:
Entry A : -> 01.01.2014 - 07.06.2014 , 00:00 - 07:15 , Value: 450 Entry A : -> 01.01.2014 - 07.06.2014 , 07:30 - 15:30 , Value: 230 Entry A : -> 01.01.2014 - 07.06.2014 , 15:45 - 23:45 , Value: 300 Entry B : -> 01.01.2014 - 07.06.2014 , 00:00 - 02:15 , Value: 130 Entry B : -> 01.01.2014 - 07.06.2014 , 02:30 - 10:45 , Value: 250 Entry B : -> 01.01.2014 - 07.06.2014 , 11:00 - 23:45 , Value: 340 Entry C ...
For each datetime in the range there is stored one value in 15 minutes per step:
For example the database should looks like this:
01.01.2014 00:00:00 | 450 | Entry A 01.01.2014 00:15:00 | 450 | Entry A .. .. 01.01.2014 07:15:00 | 450 | Entry A 01.01.2014 07:30:00 | 230 | Entry A 01.01.2014 07.45:00 | 230 | Entry A .. .. 01.01.2014 15:30:00 | 230 | Entry A 01.01.2014 15:45:00 | 300 | Entry A 01.01.2014 16:00:00 | 300 | Entry A .. .. 01.01.2014 23:45:00 | 300 | Entry A 01.01.2014 07:30:00 | 300 | Entry A .. .. 01.01.2014 07:15:00 | 230 | Entry A 01.01.2014 07:30:00 | 230 | Entry A .. .. 02.01.2014 ...
The same for every entry and for every day till the end.
I think the problem are the many inserts. Over five month for three entries, every day, every 15 minutes.
I wrote two scripts, both are working. But it takes a long time to save the entries.
Script 1: I have executed the SQL statement directly in VB.
Dim cmd As DbCommand = New SqlCommand()
cmd.CommandType = Data.CommandType.Text
Temp2 = DateTime.Parse(DateStart+ " " + TimeStart)
Temp3 = DateTime.Parse(DateBegin+ " " + TimeBegin)
While (Temp2 <= Temp3)
Date = Temp2
Temp1 = Temp3
While (Temp <= Temp1)
cmd.CommandText = "INSERT INTO [dbo].[_Table_Data]([pr_id],[pr_Nr],[Date],[Value],[nvv]) VALUES (" & pr_id & "," & pr_nr & ", '" & Date & "', " & Value & " , '" & Entry &"')"
cmd.Connection = con
con.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader
While (dr.Read())
' save entrys
End While
con.Close()
Temp = DateAdd(DateInterval.Minute, 15, Temp)
End While
Temp2 = DateAdd(DateInterval.Day, 1, Temp2)
End While
The second script was a SQL procedure, I sent the values and dates to a SQL procedure. But no success, worse as the first script.
I also tried a CTE
;with cte as
(
select DATEADD(MONTH,DATEDIFF(MONTH,0,@StDate),0) as Sdate
union all
select DATEADD(MINUTE,15,SDate) from cte where SDate < @Enddate
)
But in this case I only have one Startdate and one Enddate. If i have to use 01.01.2014 - 06.06.2014 with one value, it works great. But for different times, i can't use it.
Please help, I need a quite fast solution.