I have an Procedure that receives an specific date as parameter ie Exec ProcDB '20150428'
frequently I need to run this procedure for many dates and usually I retype Exec ProcDB 'date1' GO Exec ProcDB 'date2'go..... I think it's not smart, so
I can get the valid list of dates using a Select Distinct [dates] From Table1 Order By [dates].
So I want to create a new Procedure that receives Start_Dt and End_Dt and it loops for all dates that my select distinct returns where its between including Start_Dt and End_Dt.
ie something like:
Create ProcDBlist Start_Dt as date, End_Dt as date
For each date in: Select Distinct [date] from [table1] where [date] >= @Start_Dt and [date] <= @End_dt
Do: Exec ProcDB 'Date n'
End
UPDATED:
Final solution:
Create procedure [dbo].[ProcessDBRange] (@Start_dt as varchar(15) =null, @End_dt as varchar(15) =null)
As
Begin
DECLARE @date as varchar(15)
DECLARE Cursor_ProcessDB CURSOR FOR
Select Distinct Convert(varchar(15), [date], 112) as [date]
From [Prices]
Where [date] >= @Start_dt and [date] <= @End_dt
Order By [date]
OPEN Cursor_ProcessDB
FETCH next FROM Cursor_ProcessDB
INTO @date
WHILE @@FETCH_STATUS = 0
BEGIN
Exec ProcessDB @date
FETCH next FROM Cursor_ProcessDB
INTO @date
END
CLOSE Cursor_ProcessDB
DEALLOCATE Cursor_ProcessDB
End