I have a query as shown below. Currently, @ThisDate is set to 1 so that when the query executes the Date parameter is set to GETDATE()-1. If @ThisDate is set to 2 then the Date parameter would be GETDATE()-2 and so on.I need to let the user pass a Loop value parameter and then somehow Loop the code from 1 to the users specified amount, whilst changing the @ThisDate value to reflect the current Loop iteration.So, lets say the user passes 3 into a LoopCount parameter. The code would loop 3 times with the first loop setting @ThisDate to 1, the 2nd setting it to 2 and the 3rd setting @ThisDate to 3 then finishing. Is this possible?DECLARE @ThisDate IntTRUNCATE TABLE LBMM_AgentCounter --Only at the beginningSELECT @ThisDate = 1INSERT LBMM_AgentCounter(AgentId, DateofCall, Agent, NoofCalls, SM)SELECT distinct A.agentid, A.[dateofcall], A.agentname as 'Agent', A.noofcallsmade as 'NoofCalls', ISNULL(SM.[Name], 'UNKNOWN') as 'SM'FROM (select distinct dateofcall, ma.agentid, agentname, count(*) as noofcallsmade from callrslt_archive ca inner join magent ma on ca.agentid=ma.agentid where dateofcall = convert(int, convert(varchar, getdate()-@ThisDate, 112))and (ma.agentid in (SELECT distinct dbo.LBMM_Agents.diallerid FROM dbo.LBMM_Agents INNER JOIN dbo.LBMM_TimesheetFact ON dbo.LBMM_Agents.keyid = dbo.LBMM_TimesheetFact.AgentIDwhere [date]=convert(int, convert(varchar, getdate()-@ThisDate, 112)) AND (ProductionHours = 0 AND OvertimeHours = 0) )or ma.agentid not in(SELECT distinct dbo.LBMM_Agents.diallerid FROM dbo.LBMM_Agents INNER JOIN dbo.LBMM_TimesheetFact ON dbo.LBMM_Agents.keyid = dbo.LBMM_TimesheetFact.AgentIDwhere [date]=convert(int, convert(varchar, getdate()-@ThisDate, 112)))) group by ma.agentid, agentname,dateofcallhaving count(*)>1) Aleft Join LBMM_Agents on A.agentid = LBMM_Agents.diallerid and LBMM_Agents.dateid = ( select max(dateid)from LBMM_Agents)left join SM SM on LBMM_Agents.parentkeyid = SM.KeyID WHERE ISNULL(RIGHT(SM.[Name],12), 'UNKNOWN') <>'[Historical]'order by agentname