$tables = @("ProductType","CurrencyMaster", "Cards", "Transaction", "Transaction_Int", "Payment_Token") $initCmd = @("ALTER TABLE AlexisPTSTest.dbo.AUTH_IMPLIED_LIMIT NOCHECK CONSTRAINT all; ") $initCmd += "ALTER TABLE AlexisPTSTest.dbo.Payment_Token NOCHECK CONSTRAINT all; " $initCmd += "ALTER TABLE AlexisPTSTest.dbo.TxnAccumulator NOCHECK CONSTRAINT all; " $initCmd += "ALTER TABLE AlexisPTSTest.dbo.BNK_Banking_ModulrDetails NOCHECK CONSTRAINT all; " $initCmd += "ALTER TABLE AlexisPTSTest.dbo.BNK_BankAccountNumbers NOCHECK CONSTRAINT all; " $initCmd += "ALTER TABLE AlexisPTSTest.dbo.AccumulatorProductLimit NOCHECK CONSTRAINT all; " $initCmd += "ALTER DATABASE AlexisPTSTest SET RECOVERY SIMPLE; " $finalCmd = @() for ($i=0; $i -lt $tables.length; $i++) { $table = $tables[$i] $initCmd += "DECLARE @Deleted_Rows INT; SET @Deleted_Rows = 1; WHILE (@Deleted_Rows > 0) BEGIN DELETE TOP (100000) FROM AlexisPTSTest.dbo.[${table}]; SET @Deleted_Rows = @@ROWCOUNT; END " $initCmd += "SET IDENTITY_INSERT AlexisPTSTest.dbo.[${table}] ON; " $finalCmd += "SET IDENTITY_INSERT AlexisPTSTest.dbo.[${table}] OFF; " } $finalCmd += "SET IDENTITY_INSERT AlexisPTSTest.dbo.[Transaction] ON; DELETE FROM AlexisPTSTest.dbo.[Transaction_Int] WHERE EXISTS (SELECT 1 FROM AlexisPTSTest.dbo.[Transaction] t WHERE t.Id = AlexisPTSTest.dbo.[Transaction_Int].Id); INSERT INTO AlexisPTSTest.dbo.[Transaction] ([ID], [PAN], [SysDate], [TransDate], [TransCcy], [BillAmt], [Description], [OldStatus], [NewStatus], [TransactionType], [RecordID], [TransactionStatus], [TransAmt], [BillNat_Conv], [Actual_Balance], [Blocked_Amount], [flCredit], [ProcessingCode], [AuthMethod], [LoadSRC], [LoadType], [OrigStan], [Note], [Amount_other], [Fixed_Fee], [Rate_Fee], [Fx_Padding], [Mcc_Padding], [AuthPresDiff], [txnCountry], [FeeID], [Trans_Link], [SettlementAmt], [Additional_Data], [BillCcy], [SettleCcy], [TxnTypeProcCode], [Account_From], [Account_To], [traceid_lifecycle], [payment_token_id]) SELECT * FROM AlexisPTSTest.dbo.[Transaction_Int] WHERE transDate > '2020-01-01'; DELETE FROM AlexisPTSTest.dbo.[Transaction_Int] WHERE transDate > '2020-01-01'; SET IDENTITY_INSERT AlexisPTSTest.dbo.[Transaction] OFF;" $finalCmd += "USE AlexisPTSTest; DBCC SHRINKFILE (Alexis, 1); DBCC SHRINKFILE (Alexis_log, 1); ALTER DATABASE AlexisPTSTest SET RECOVERY FULL;" for ($i=0; $i -lt $initCmd.Count; $i++) { $cmd = $initCmd[$i] Write-Host $cmd sqlcmd -S localhost -U gpspl -P yW2S6KXdY8 -Q "${cmd}" } for ($i=0; $i -lt $tables.length; $i++) { $table = $tables[$i] $bcpIn = "AlexisPTSTest.dbo.[${table}] in ${table}.bcp -f ${table}.fmt -e ${table}.log -E -b 50000 -S localhost -U gpspl -P yW2S6KXdY8" Start-Process bcp -ArgumentList $bcpIn -Wait } for ($i=0; $i -lt $finalCmd.Count; $i++) { $cmd = $finalCmd[$i] Write-Host $cmd sqlcmd -S localhost -U gpspl -P yW2S6KXdY8 -Q "${cmd}" }