I have 4 tables. Table1 ID
is the primary key and Table2 ID
the foreign . Table3 and Table4 are basically archives of Table1 and Table2 and have the same primary key and foreign key and columns.
Table 1
:
ID | Date | Amount
Table 2
:
ID | Type
I had to archive table 1
data to table 3
based on a defined value in Date which I did using this:
Declare @Date date
set @Date = '2021-11-18'
Begin transaction
INSERT INTO dbo.Table3 (ID, Date, Amount)
SELECT *
FROM dbo.Table1
WHERE Date=@Date
Executing this code resulted in adding this to Table3:
52 | 2021-11-18 | 12
53 | 2021-11-18 | 14
The question is how do I now archive table2 data to table4? I only need the data from table2 that matches with how we filtered the Date. Basically, copy all table2 data to table4 that matches the ID
in table3.