你好我上周要求在sql db中存档一些表,我是SQL的新手,我在其他问题中看到了一些代码。任何人都可以解释声明@nextid表和后续代码中的唯一ID,代码不是我的,并且信用是给予所有者的。
在我的情况下,我有一个主键调用attachmentID,在db调用prod中名为附件的表中,日期列是attachmentdate。我不确定我是要更换UniqueID还是什么?如果可以,请帮忙。
DECLARE @NextIDs TABLE(UniqueID int primary key)
DECLARE @ThreeYearsAgo datetime
SELECT @ThreeYearsAgo = DATEADD(d, -(3 * 365), GetDate())
WHILE EXISTS(SELECT 1
FROM [ISAdminDB].[dbo].[MyTable]
WHERE [MyTable].[DateTime] < @ThreeYearsAgo)
BEGIN
BEGIN TRAN
INSERT INTO @NextIDs(UniqueID)
SELECT TOP 1000 UniqueID
FROM [ISAdminDB].[dbo].[MyTable]
WHERE [MyTable].[DateTime] < @ThreeYearsAgo
INSERT INTO [ISArchive].[dbo].[MyTable] (<Fields>)
SELECT (<Fields>)
FROM [ISAdminDB].[dbo].[MyTable] AS a
INNER JOIN @NextIDs AS b ON a.UniqueID = b.UniqueID
DELETE [ISAdminDB].[dbo].[MyTable]
FROM [ISAdminDB].[dbo].[MyTable]
INNER JOIN @NextIDs AS b ON a.UniqueID = b.UniqueID
DELETE FROM @NextIDs
COMMIT TRAN
END
答案 0 :(得分:0)
@NextIDs是一个表变量。 这里数据从isadmin mytable移动到表变量@NextIDs 然后从@NextIDs到isarchive mytable 之后,isadmin mytable数据已被删除