Hi @kkran Check this:
CREATE PROC SP_Temp
AS
BEGIN
DECLARE @Snapshot DATETIME = GETDATE()
IF NOT EXISTS(SELECT * FROM [USDW].[dbo].[Table1] WHERE CONVERT(DATE,SnapShotDatetime)=CONVERT(DATE,@Snapshot))
BEGIN
INSERT INTO [USDW].[dbo].[Table1] (column1, column2, column3, column4, column5, column6 ,SnapShotDatetime)
SELECT column1, column2, column3, column4, column5, column6 ,@Snapshot
FROM OPENQUERY ( Linkedserver, 'Select * from LinkedTable')
WHERE CONVERT(DATE,SnapShotDatetime)=CONVERT(DATE,@Snapshot)
END
DELETE [USDW].[dbo].[Table1] WHERE CONVERT(DATE,SnapShotDatetime) < CONVERT(DATE, DATEADD(month, -12, GETDATE()))
END
EXEC SP_Temp
Best regards,
Cosmog Hong
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.