I have records in my Sql Server 2008 table, I need to update the field TranTime. How can I do it using a loop? Evey iteration should insert/update a unique time.
I don't know the syntax/approach. Any help would be appreciated.
I have records in my Sql Server 2008 table, I need to update the field TranTime. How can I do it using a loop? Evey iteration should insert/update a unique time.
I don't know the syntax/approach. Any help would be appreciated.
 
    
    Update statement by using DateTime.Now:
SqlCommand cmd = new SqlCommand("Update yourTableName set TranTime = @dt",yourConnection);
SqlParameter parameter = cmd.Parameters.Add("@dt", System.Data.SqlDbType.DateTime);
parameter.Value = DateTime.Now;
If you want to update it by time only and unique you could use TimeOfDay. Like this: 
DateTime.Now.TimeOfDay
Or ToLongTimeString like this:
DateTime.Now.ToLongTimeString()
Based on your needs.
 
    
    I have come up with this solution
    DECLARE @ID INT
    DECLARE @T int
    DECLARE @Time time(7)
    SET @ID = 2
    set @T = 80005836 //Around 8:AM
    WHILE (@ID <=15) //I want to update 15 rows only
    BEGIN
   SELECT (@T / 1000000) % 100 as hour,
          (@T / 10000) % 100 as minute,
          (@T / 100) % 100 as second,
          (@T % 100) * 10 as millisecond
   SET @Time = 
   (
   SELECT dateadd(hour, (@T / 1000000) % 100,
   dateadd(minute, (@T / 10000) % 100,
   dateadd(second, (@T / 100) % 100,
   dateadd(millisecond, (@T % 100) * 10, cast('00:00:00' as time(2)))))) AS Time 
   )
     UPDATE myTable
     SET TranTime = @Time
     WHERE ID = @ID
    //increment ctr for ID and add 1 hour for a Time
    SET @ID = @ID + 1
    SET @T = @T + 1000000
    END
    GO
