Sql Scripts for Table copy
	Inserting a column in a management table for new entry:
    
    $tb = Table name

         INSERT INTO [MANAGEMENT].[dbo].[Management_table] (ReqID,DatabaseName, Table_Name,         dateandtime) VALUES ((SELECT (max(ReqID)+1) from [MANAGEMENT].[dbo].[Management_table]),   'vinci100295', '$(tb)', CURRENT_TIMESTAMP );

	Number of rows before copy:
    $ip: Server name
    $tb: Table name

update [MANAGEMENT].[dbo].[Management_table] set rowcount_before_copy= (select count(*) from [$(ip)].[vinci100295].[dbo].[$(tb)]) where ReqID = (select MAX(ReqID) from [MANAGEMENT].[dbo].[Management_table])


	Number of rows after copy:
    
     $tb = Table name

update [MANAGEMENT].[dbo].[Management_table] set rowcount_after_copy= (select count(*) from [vinci100295].[dbo].[$(tb)]) where ReqID = (select MAX(ReqID) from [MANAGEMENT].[dbo].[Management_table])

	Checksum Before copy:
    
   $tb = Table name

update Management_table set checksum_before_copy = (select  SUM( CAST( checksum(*) AS BIGINT )) from [$(ip)].[vinci100295].[dbo].[$(tb)]) where ReqID = (select MAX(ReqID) from [MANAGEMENT].[dbo].[Management_table])

	Checksum after copy:
    
     $tb = Table name

   update Management_table set checksum_after_copy = (select  SUM( CAST( checksum(*) AS BIGINT )) from [vinci100295].[dbo].[$(tb)]) where ReqID = (select MAX(ReqID) from [MANAGEMENT].[dbo].[Management_table])


	Status before copy if process completed:
    
update [MANAGEMENT].[dbo].[Management_table] set status_before_copy = 'Completed' where ReqID = (select MAX(ReqID) from [MANAGEMENT].[dbo].[Management_table])

	Status before copy if process failed:
update [MANAGEMENT].[dbo].[Management_table] SET status_before_copy = 'Copy Failed'
WHERE ReqID = (select MAX(ReqID) from [MANAGEMENT].[dbo].[Management_table])

	Table copy:
    $ip: Server Name
    $tb = Table name

SELECT * INTO [vinci100295].[dbo].[$(tb)]
FROM [$(ip)].[vinci100295].[dbo].[$(tb)]

	Status after copy  if process completed:
    
update [MANAGEMENT].[dbo].[Management_table] set status_after_copy = 'Completed' where ReqID = (select MAX(ReqID) from [MANAGEMENT].[dbo].[Management_table]);

	Status after copy  if process failed:
    
   update [MANAGEMENT].[dbo].[Management_table] SET status_after_copy = 'Copy Failed' 
WHERE ReqID = (select MAX(ReqID) from [MANAGEMENT].[dbo].[Management_table])


	Copy Status to check copy successful or copy corrupted
    
UPDATE [MANAGEMENT].[dbo].[Management_table] 
SET CopyStatus =
CASE
(select checksum_before_copy - checksum_after_copy
from [MANAGEMENT].[dbo].[Management_table]
where ReqID = (select MAX(ReqID) from [MANAGEMENT].[dbo].[Management_table]))
when 0 THEN 'Copy Successful'
ELSE 'Copy Corrupted'
END
WHERE ReqID = (select MAX(ReqID) from [MANAGEMENT].[dbo].[Management_table])


	Drop table if table copy is corrupted:
    
     $tb = Table name

IF EXISTS(SELECT Table_Name FROM [MANAGEMENT].[dbo].[Management_table] where CopyStatus = 'Copy Corrupted' and ReqID = (select max(ReqID) from  [MANAGEMENT].[dbo].[Management_table])) 
drop table [vinci100295].[dbo].[$(tb)]


	Copy Status if copy successful
update [MANAGEMENT].[dbo].[Management_table] set CopyStatus = 'Copy Successful' where ReqID = (select MAX(ReqID) from [MANAGEMENT].[dbo].[Management_table])


