Running a SQL Stored Procedure in a Loop

  • The Stored Procedure has a date parameter.

    myProc should run in a loop while the date is between '2010-01-06' and '2010-01-25'.

    I dont want to add anything inside the stored procedure proc_student,

    would prefer if just running it from a query outside the stored procedure proc_student.

    Please view code below and provide any suggestions, thanks.

    ..............................................................

    CREATE TABLE Student (

    Id int,

    Name varchar(20),

    Class int,

    Date datetime

    )

    ..............................................................

    insert into Student values(1, 'Komal',10,'2010-01-05');

    insert into Student values(2, 'Ajay',10,'2010-01-07');

    insert into Student values(3, 'Santosh',10,'2010-01-10');

    insert into Student values(4, 'Rakesh',10,'2010-01-28');

    insert into Student values(5, 'Bhau',10,'2010-01-17');

    ..............................................................

    CREATE PROCEDURE proc_student

    @Date datetime

    AS

    Select * from Student

    where Date = @Date

    ..............................................................

    exec proc_student @Date = '2010-01-05'

    ..............................................................

    Below is the external query to execute Stored Procedure in a Loop:

    DECLARE @Date datetime

    DECLARE @MaxDate datetime

    SET @Date = '2010-01-05'

    SET @MaxDate = '2010-01-25'

    WHILE @Date < @MaxDate

    BEGIN

    SET @Date = @Date + 1

    IF @Date = '2010-01-06' OR

    @Date = '2010-01-07' OR

    @Date = '2010-01-11' OR

    @Date = '2010-01-20' OR

    @Date = '2010-01-25'

    EXEC proc_student @Date

    END

  • Since you are determined to run it in a loop, this is as good a method as any other.

    BTW, you ought to remove your "IF" statement... with it, you're not getting all dates.

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • Why a loop?

    Why not just the following:

    EXEC proc_student '2010-01-06'

    EXEC proc_student '2010-01-07'

    EXEC proc_student '2010-01-11'

    EXEC proc_student '2010-01-20'

    EXEC proc_student '2010-01-25'

    Rob Schripsema
    Propack, Inc.

  • /*

    The Stored Procedure has a date parameter.

    myProc should run in a loop while the date is between '2010-01-05' and '2010-01-25',

    so date '2010-01-28' will be skipped.

    I dont want to add anything inside the stored procedure proc_student,

    would prefer if just running it from a query outside the stored procedure proc_student.

    This example works ok, but I am looking for other methods to do the same.

    Please view code below and provide any suggestions, thanks.

    */

    CREATE TABLE Students (

    Id int,

    Name varchar(20),

    Class int,

    Date datetime

    )

    --------------------------------------------------------------

    insert into Students values(1, 'Goran',10,'2010-01-05');

    insert into Students values(2, 'Nikolai',10,'2010-01-07');

    insert into Students values(3, 'Oksana',10,'2010-01-10');

    insert into Students values(4, 'Peter',10,'2010-01-17');

    insert into Students values(5, 'Steven',10,'2010-01-28');

    --------------------------------------------------------------

    CREATE PROCEDURE proc_students

    @Date datetime

    AS

    Select * from Students

    where Date = @Date

    --------------------------------------------------------------

    exec proc_students @Date = '2010-01-17'

    --------------------------------------------------------------

    --Below is the external query to execute Stored Procedure in a Loop:

    DECLARE @Date datetime

    DECLARE @MaxDate datetime

    SET @Date = '2010-01-04'

    SET @MaxDate = '2010-01-25'

    WHILE @Date < @MaxDate

    BEGIN

    SET @Date = @Date + 1

    IF @Date = '2010-01-05'

    OR @Date = '2010-01-07'

    OR @Date = '2010-01-10'

    OR @Date = '2010-01-17'

    OR @Date = '2010-01-25'

    EXEC proc_students @Date

    END

  • clive-421796 (4/15/2010)


    myProc should run in a loop while the date is between '2010-01-06' and '2010-01-25'.

    if there is Date range then

    below should work

    CREATE PROCEDURE proc_student

    @Start_Date datetime,

    @end_date datetime

    AS

    Select * from Student

    where Date between @Start_Date AND @end_date

    GO

    EXEC proc_student '2010-01-02', '2010-05-01'

    -------Bhuvnesh----------
    I work only to learn Sql Server...though my company pays me for getting their stuff done;-)

  • Hi Bhuvnesh, I think you misunderstood my post.

    The Stored Procedure has one date parameter (UpdateDate).

    Want to execute the stored procedure in a loop while the UpdateDate is between

    '2010-01-05' and '2010-01-25'.

  • My recommendation would be to rewrite the proc_student stored proc to use set based code instead of RBAR. However, I do understand that that is sometimes just not possible. With that in mind, let's not even think of using a cursor or While Loop. Instead, let's use some of the features of SQL Server 2005 to keep the code short, fast, and sweet. BTW... thanks for posting your data the way you did... made it real easy to figure this out...

    DECLARE @SQL VARCHAR(MAX)

    SELECT @SQL = (

    SELECT 'EXEC proc_students '

    + QUOTENAME(CONVERT(CHAR(10),Date,120),'''')

    + CHAR(10)

    FROM dbo.Students

    FOR XML PATH('')

    )

    PRINT @SQL -- This just shows what the final command will be and can be commented out

    --EXEC (@SQL) -- This executes the command(s) we just built. Just uncomment it when you're done testing.

    {edit} P.S. If you really want to process all dates even though a student doesn't appear on a given date, post back and I'll show you another little trick with a thing called a "Tally" table. 😉

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply