0
我有一個約700萬行的表,我需要執行截斷的表。我會這樣做,如下所示:SQL Server創建表變量來保存將截斷永久表後將插入到永久表中的記錄
BEGIN TRY
BEGIN TRANSACTION
Declare @RecsToKeep Table
(
Id int
)
SELECT Id
FROM RealTable
Where CONVERT (DATE, CreatedDate) > '2017-08-16'
Declare @KeepTheseRecs Table
(
Id int
)
Insert into @KeepTheseRecs
Select *
From RealTable Where Id IN (Select Id From @RecsToKeep)
Truncate Table RealTable
Insert into RealTable
Select *
From @KeepTheseRecs
COMMIT
END TRY
BEGIN CATCH
ROLLBACK
END CATCH
真正的表和表變量具有相同的列結構。這是做到這一點的正確方法嗎?
如果插入失敗並且丟失數據會發生什麼情況?至少沒有交易似乎是危險的。我認爲審計表值得考慮。 –