I am trying to do some complex manipulations in SQL Server. To prevent any performance penalty, I am trying to do all the manipulations on database side.
Below is my query in C#
"DECLARE @t AS TABLE (StartTime DATETIME, EndTime DateTime, Value REAL, V1 REAL, V2 REAL) " +
"DECLARE @i AS DateTime = @startDate " +
"WHILE @i <= @endDate " +
"BEGIN " +
"DECLARE @min AS Real = " +
"(SELECT TOP 1 RawStatus FROM CustomPollerStatistics_Detail " +
"WHERE RawStatus IS NOT NULL AND CustomPollerAssignmentID = @pollerID " +
"AND [DateTime] <= @i ORDER BY [DateTime] DESC) " +
"DECLARE @max AS Real = " +
"(SELECT TOP 1 RawStatus FROM CustomPollerStatistics_Detail " +
"WHERE RawStatus IS NOT NULL AND CustomPollerAssignmentID = @pollerID " +
"AND [DateTime] <= DATEADD(MI, @interval, @i) ORDER BY [DateTime] DESC) " +
"INSERT INTO @t VALUES (@i, DATEADD(MI, @interval, @i), @max - @min, @min, @max) " +
"SET @i = DATEADD(MI, @interval, @i) " +
"END " +
"SELECT * FROM @t";
I double checked the value for parameters in debug mode. I tried running the query with same parameters by declaring it on the top of the query with same values in SQL Server Management Studio which are being passed from C#, in this case, it produce correct result as expected. I have added parameters in following way.
com.Parameters.Add("@pollerID", System.Data.SqlDbType.UniqueIdentifier).Value = assignmentID;
com.Parameters.Add("@interval", System.Data.SqlDbType.Int).Value = accuracyMinutes;
com.Parameters.Add("@startDate", System.Data.SqlDbType.DateTime).Value = startDate;
com.Parameters.Add("@endDate", System.Data.SqlDbType.DateTime).Value = endDate;
When I run this query from C#, the columns @max - @min, @min, @max
(3rd, 4th and 5th column) all evaluates to 0
always. I am not being able to find out the reason for this on my own. Please help. See screenshot below.
Sample Data http://pastebin.com/raw.php?i=QuBCSRw3. C# Code http://pastebin.com/raw.php?i=9wYfT0y8.