如果更新查询失败,我需要插入值

时间:2015-06-16 12:04:45

标签: sql sql-server database

我使用以下更新查询,如果更新查询失败,我需要插入相同的数据。如何遍历和检查更新状态,如果失败更新将值插入表中......请帮助我

 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

0 个答案:

没有答案