Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Running a SQL Stored Procedure in a Loop


Running a SQL Stored Procedure in a Loop

Author
Message
kevin_nikolai
kevin_nikolai
SSC-Enthusiastic
SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)

Group: General Forum Members
Points: 189 Visits: 511
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
WayneS
WayneS
SSCertifiable
SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)

Group: General Forum Members
Points: 6230 Visits: 10398
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, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings

Rob Schripsema
Rob Schripsema
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2319 Visits: 11042
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.
kevin_nikolai
kevin_nikolai
SSC-Enthusiastic
SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)

Group: General Forum Members
Points: 189 Visits: 511
/*

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
Bhuvnesh
Bhuvnesh
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2924 Visits: 4076
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;-)
kevin_nikolai
kevin_nikolai
SSC-Enthusiastic
SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)

Group: General Forum Members
Points: 189 Visits: 511
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'.
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44994 Visits: 39880
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search