CREATE TABLE #TableConfig (ID int identity(1,1), ServerName varchar(100), DatabaseTable varchar(100), KeyColumn varchar(50), WhereFilter varchar(200)) INSERT INTO #TableConfig ( ServerName, DatabaseTable, KeyColumn, WhereFilter ) SELECT 'ServerUS1', 'DB1.dbo.ClaimInfo' , 'ClaimNo' , 'TermDt< dateadd(yy,-5,getdate())' UNION SELECT 'ServerUS2', 'DB2.dbo.MemInfo' , 'MemNum' , 'TermDt< dateadd(yy,-5,getdate())' UNION SELECT 'ServerUS3', 'DB9.dbo.CandInfo' , 'CandNum' , 'TermDt< dateadd(yy,-5,getdate())' UNION SELECT 'ServerUS4', 'DB8.dbo.ProvInfo' , 'PRovNum' , 'TermDt< dateadd(yy,-5,getdate())' SELECT * FROM #TableConfig tc
Just created this Temp as an example. My scenario, i have a physical table with hundreds of rows.
Requirement is to Loop through all the rows from this TableConfig table.
Go to first row, look for ServerName value and then connect to that server and construct SQL like
" SELECT KeyColumn FROM DatabaseTable WHERE WhereFilter " and run this and INSERT data into Target table.---- KeyColumn, DatabaseTable and WhereFilter are from same Configtable
Then go to 2nd,3rd and so on and do same thing..
Any help is appreciated. Thanks