简体   繁体   English

SQL,如何使用每个作为过程的参数循环日期列表?

[英]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_DtEnd_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.

相关问题 如何通过 sql 查询使用 for 循环通过将 python 列表作为参数或参数传递给该 sql 查询来获取特定日期的每小时数据? - How to fetch hourly data of particular date through sql query using for loop by passing python list as an argument or parameter to that sql query? 如何遍历 javascript 存储过程雪花中的日期字符串参数 - how to loop through date string parameter in javascript stored procedure snowflake 在SQL过程中使用for循环 - Using a for loop in sql procedure 如何通过减去几天到SQL存储过程将参数作为日期传递? - How to pass parameter as date by subtracting days to a SQL stored procedure? 如何传递字符串列表的sql参数以删除存储过程? - How to pass a list of strings a sql parameter for the stored procedure to delete? 如何在 SQL 的存储过程中将字符串列表作为参数传递? - How to pass a list of strings as a parameter in a stored procedure in SQL? SQL过程-使用参数作为运算符 - SQL Procedure - Using a parameter as an operator 将列表代替sql参数传递给存储过程 - Passing a list in place of sql parameter to a stored procedure 如何使用数据表和每个循环填充列表? - How to populate List using DataTable and For Each Loop? 如何使用 Python 遍历 SQL 代码,其中日期每次循环更改一个月并附加到前一个 - How do I loop over an SQL code using Python where the date changes by one month each loop and appends to the previous
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM