[英]SQL, How to loop for a date list using each as parameter for an Procedure?
I have an Procedure that receives an specific date as parameter ie Exec ProcDB '20150428' 我有一个过程接收特定日期作为参数,即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 经常我需要在很多日期运行这个程序,通常我重新输入Exec ProcDB'date1'GO Exec procDB'date2'go .....我觉得它不聪明,所以
I can get the valid list of dates using a Select Distinct [dates] From Table1 Order By [dates] . 我可以使用选择区别[日期]从表1按[日期]排序获取有效的日期列表。
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. 所以我想创建一个接收Start_Dt和End_Dt的新过程,它循环我select select distinct返回的所有日期,包括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
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
You will want to use a cursor. 您将需要使用游标。 I believe this is a good resource: http://www.codeproject.com/Tips/277847/How-to-use-Cursor-in-Sql
我相信这是一个很好的资源: http : //www.codeproject.com/Tips/277847/How-to-use-Cursor-in-Sql
I tried to make an example with the info you provided. 我试着用你提供的信息做一个例子。
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
This can be accomplished by using a cursor. 这可以通过使用游标来完成。
Basically, it goes like this: 基本上,它是这样的:
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
Edit 编辑
I've just read the link that zimdanen gave you in the comments, I must say I think in this case it may be better than a using a cursor. 我刚刚阅读了zimdanen在评论中给你的链接 ,我必须说我认为在这种情况下它可能比使用游标更好。
Edit #2 编辑#2
First, change OPEN sub
to OPEN cursor_name
. 首先,将
OPEN sub
更改为OPEN cursor_name
。 Second, use CONVERT to get the date as a string. 其次,使用CONVERT将日期作为字符串。 Make sure you convert with the correct style, otherwise you are prone to get incorrect dates and/or exceptions.
确保使用正确的样式进行转换,否则您很容易得到错误的日期和/或例外。
You can do it with cursor. 你可以用光标做到这一点。 Also you can alter proc to receive 2 parameters
@sd date, @ed date
then in proc do a loop: 你也可以改变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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.