Select "TrucateTable" , "InsertIntoTable" from dbo.DynamicTable
DECLARE @SQL VARCHAR(MAX);
DECLARE @SelectFromTable VARCHAR(128) = 'AAAfiles';
DECLARE @InsertIntoTable VARCHAR(128) = 'BalanceAmtTable';
SET @SQL = 'TRUNCATE TABLE dbo.' + @InsertIntoTable +'; INSERT INTO dbo.' + @InsertIntoTable + ' (Amt, BalanceAmt, Reasons, DateDue) SELECT Amt, BalanceAmt, Reasons, DateDue FROM dbo.' + @SelectFromTable + ' WHERE BalanceAmt >= Dateadd(Month, Datediff(Month, 0, DATEADD(m, -3, current_timestamp)), 0) AND Amt = 0;'
EXECUTE (@SQL);
If you put it into a procedure, you can pass @SelectFromTable and @InsertIntoTable as parameters and remove them from the code above.
TRUNCATE TABLE dbo.BalanceAmtTable
INSERT INTO dbo.BalanceAmtTable(Amt,BalanceAmt,Reasons,DateDue)
SELECT Amt, BalanceAmt, Reasons, DateDue
FROM dbo.AAAfiles
WHERE BalanceAmt >= Dateadd(Month, Datediff(Month, 0, DATEADD(m, -3, current_timestamp)), 0) AND Amt = 0
DECLARE @SQL VARCHAR(MAX);
DECLARE @SelectFromTable VARCHAR(128) = 'AAAfiles';
DECLARE @InsertIntoTable VARCHAR(128) = 'BalanceAmtTable';
DECLARE csrTables CURSOR LOCAL FAST_FORWARD FOR
SELECT SelectFromTable,
InsertIntoTable
FROM dbo.DynamicTable
ORDER BY SelectFromTable;
OPEN csrTables;
FETCH NEXT FROM csrTables INTO @SelectFromTable, @InsertIntoTable
WHILE @@FETCH_STATUS = 0
BEGIN
SET @SQL = 'TRUNCATE TABLE dbo.' + @InsertIntoTable +'; INSERT INTO dbo.' + @InsertIntoTable + ' (Amt, BalanceAmt, Reasons, DateDue) SELECT Amt, BalanceAmt, Reasons, DateDue FROM dbo.' + @SelectFromTable + ' WHERE BalanceAmt >= Dateadd(Month, Datediff(Month, 0, DATEADD(m, -3, current_timestamp)), 0) AND Amt = 0;'
EXECUTE (@SQL);
-- if there is other processing to do against the table you just loaded, it would go here
FETCH NEXT FROM csrTables INTO @SelectFromTable, @InsertIntoTable
END
CLOSE csrTables
DEALLOCATE csrTables
-- post processing
DECLARE @SQL VARCHAR(MAX);
DECLARE @SelectFromTable VARCHAR(128) = 'AAAfiles';
DECLARE @InsertIntoTable VARCHAR(128) = 'BalanceAmtTable';
DECLARE csrTables CURSOR LOCAL FAST_FORWARD FOR
SELECT SelectFromTable,
InsertIntoTable
FROM dbo.DynamicTable
ORDER BY SelectFromTable;
OPEN csrTables;
FETCH NEXT FROM csrTables INTO @SelectFromTable, @InsertIntoTable
IF @@FETCH_STATUS = 0
EXECUTE ('TRUNCATE TABLE dbo.' + @InsertIntoTable +'; ')
WHILE @@FETCH_STATUS = 0
BEGIN
SET @SQL = 'INSERT INTO dbo.' + @InsertIntoTable + ' (Amt, BalanceAmt, Reasons, DateDue) SELECT Amt, BalanceAmt, Reasons, DateDue FROM dbo.' + @SelectFromTable + ' WHERE BalanceAmt >= Dateadd(Month, Datediff(Month, 0, DATEADD(m, -3, current_timestamp)), 0) AND Amt = 0;'
EXECUTE (@SQL);
-- if there is other processing to do against the table you just loaded, it would go here
FETCH NEXT FROM csrTables INTO @SelectFromTable, @InsertIntoTable
END
CLOSE csrTables
DEALLOCATE csrTables
-- post processing
Src Amt BalanceAmt Reasons DateDue
AAA 0.00 2018-11-02 reason 2 - AAA 2018-12-31
AAA 123.45 2018-11-01 reason 1 - AAA 2018-12-31
BBB 0.00 2018-11-04 reason 2 - BBB 2018-12-31
BBB 987.65 2018-11-03 reason 1 - BBB 2018-12-31
CCC 777.77 2018-11-05 reason 1 - CCC 2018-12-31
CCC 888.88 2018-11-06 reason 2 - CCC 2018-12-31
Out 0.00 2018-11-02 reason 2 - AAA 2018-12-31
Out 0.00 2018-11-04 reason 2 - BBB 2018-12-31