I am using the following update query , I need to insert the same data, if the update query fails. How can I traverse and check the status of update and if fail updation insert the values to the table...please help me
 With CTE AS 
    (
        SELECT CONVERT(DATETIME, CONVERT(VARCHAR, new_time, 101)) As day,
               DATEPART(hh,new_time) As hour,
               COUNT(*) As Total
        FROM log_table 
        WHERE new_time > GETDATE() - 180
        GROUP BY  CONVERT(DATETIME, CONVERT(VARCHAR, new_time, 101)),
                  DATEPART(dd,new_time),
                  DATEPART(hh,new_time)
    )
    UPDATE tmp_table
    SET Count= CTE.Total
    FROM tmp_table INNER JOIN CTE
         ON temp_table.date=cte.day AND temp_table.hour=cte.hour
