SQL,如何循环使用每个日期列表作为过程的参数?
SQL, How to loop for a date list using each as parameter for an Procedure?
我有一个接收特定日期作为参数的程序
即 Exec ProcDB '20150428'
我经常需要 运行 这个过程很多日期,通常我重新输入 Exec ProcDB 'date1' GO Exec ProcDB 'date2'go... ..
我觉得不聪明,所以
我可以使用 Select 不同的 [dates] From Table1 Order By [dates].
获取有效的日期列表
所以我想创建一个接收 Start_Dt 和 End_Dt 的新过程
它循环所有日期,我的 select 不同 returns 其中包括 Start_Dt 和 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
更新:
最终解决方案:
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
这可以通过使用 cursor.
基本上,它是这样的:
DECLARE @Date datetime -- a local variable to get the cursor's result
DECLARE DatesCursor CURSOR FOR
Select Distinct [dates] where [dates] between @Start_Dt and @End_Dt From Table1 Order By [dates]. -- the query that the cursor iterate on
OPEN DatesCursor
FETCH NEXT FROM DatesCursor INTO @Date
WHILE @@FETCH_STATUS = 0 -- this will be 0 as long as the cursor returns a result
BEGIN
Exec ProcDB @Date
FETCH NEXT FROM DatesCursor INTO @Date -- don't forget to fetch the next result inside the loop as well!
END
-- cleanup - Very important!
CLOSE DatesCursor
DEALLOCATE DatesCursor
编辑
我刚刚阅读了 link that zimdanen 给你的评论,我必须说我认为在这种情况下它可能比使用游标更好。
编辑#2
首先,将OPEN sub
改为OPEN cursor_name
。
其次,使用 CONVERT 将日期作为字符串获取。
确保您使用正确的样式进行转换,否则您很容易得到不正确的日期 and/or 异常。
您将需要使用游标。我相信这是一个很好的资源:http://www.codeproject.com/Tips/277847/How-to-use-Cursor-in-Sql
我试着用你提供的信息做了一个例子。
DECLARE @Start_dt DATE;
DECLARE @End_dt DATE;
DECLARE @date DATE;
DECLARE cursor_name CURSOR FOR
SELECT DISTINCT Date
FROM [table1]
WHERE Date >= @Start__Dt
and Date <= @End__Dt
ORDER BY Date
OPEN cursor_name
FETCH next FROM cursor_name
INTO @date
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @date2 VARCHAR(15)
SET @date2 = (CAST ( @date AS varchar(15) ))
Exec ProcdB date_parameter_name = @date2
FETCH next FROM cursor_name
INTO @date
END
CLOSE cursor_name
DEALLOCATE cursor_name
你可以用光标来完成。您也可以更改 proc 以接收 2 个参数 @sd date, @ed date
然后在 proc 中执行循环:
alter procedure procDB
@sd date,
@ed date
as
begin
while @sd <= @ed
begin
--do your staff
set @sd = dateadd(dd, 1, @sd)
end
end
我有一个接收特定日期作为参数的程序 即 Exec ProcDB '20150428'
我经常需要 运行 这个过程很多日期,通常我重新输入 Exec ProcDB 'date1' GO Exec ProcDB 'date2'go... .. 我觉得不聪明,所以
我可以使用 Select 不同的 [dates] From Table1 Order By [dates].
获取有效的日期列表所以我想创建一个接收 Start_Dt 和 End_Dt 的新过程 它循环所有日期,我的 select 不同 returns 其中包括 Start_Dt 和 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
更新:
最终解决方案:
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
这可以通过使用 cursor.
基本上,它是这样的:
DECLARE @Date datetime -- a local variable to get the cursor's result
DECLARE DatesCursor CURSOR FOR
Select Distinct [dates] where [dates] between @Start_Dt and @End_Dt From Table1 Order By [dates]. -- the query that the cursor iterate on
OPEN DatesCursor
FETCH NEXT FROM DatesCursor INTO @Date
WHILE @@FETCH_STATUS = 0 -- this will be 0 as long as the cursor returns a result
BEGIN
Exec ProcDB @Date
FETCH NEXT FROM DatesCursor INTO @Date -- don't forget to fetch the next result inside the loop as well!
END
-- cleanup - Very important!
CLOSE DatesCursor
DEALLOCATE DatesCursor
编辑
我刚刚阅读了 link that zimdanen 给你的评论,我必须说我认为在这种情况下它可能比使用游标更好。
编辑#2
首先,将OPEN sub
改为OPEN cursor_name
。
其次,使用 CONVERT 将日期作为字符串获取。
确保您使用正确的样式进行转换,否则您很容易得到不正确的日期 and/or 异常。
您将需要使用游标。我相信这是一个很好的资源:http://www.codeproject.com/Tips/277847/How-to-use-Cursor-in-Sql
我试着用你提供的信息做了一个例子。
DECLARE @Start_dt DATE;
DECLARE @End_dt DATE;
DECLARE @date DATE;
DECLARE cursor_name CURSOR FOR
SELECT DISTINCT Date
FROM [table1]
WHERE Date >= @Start__Dt
and Date <= @End__Dt
ORDER BY Date
OPEN cursor_name
FETCH next FROM cursor_name
INTO @date
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @date2 VARCHAR(15)
SET @date2 = (CAST ( @date AS varchar(15) ))
Exec ProcdB date_parameter_name = @date2
FETCH next FROM cursor_name
INTO @date
END
CLOSE cursor_name
DEALLOCATE cursor_name
你可以用光标来完成。您也可以更改 proc 以接收 2 个参数 @sd date, @ed date
然后在 proc 中执行循环:
alter procedure procDB
@sd date,
@ed date
as
begin
while @sd <= @ed
begin
--do your staff
set @sd = dateadd(dd, 1, @sd)
end
end