Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Running a SQL Stored Procedure in a Loop Expand / Collapse
Author
Message
Posted Thursday, April 15, 2010 5:20 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, September 29, 2014 3:31 PM
Points: 161, Visits: 461
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
Post #904494
Posted Thursday, April 15, 2010 6:11 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 8:34 AM
Points: 5,365, Visits: 8,967
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
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
Post #904509
Posted Thursday, April 15, 2010 6:14 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Saturday, October 25, 2014 7:43 AM
Points: 2,010, Visits: 10,968
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
Accelitec, Inc
Post #904510
Posted Thursday, April 15, 2010 11:47 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, September 29, 2014 3:31 PM
Points: 161, Visits: 461
/*

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
Post #904566
Posted Friday, April 16, 2010 8:12 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 3:57 AM
Points: 2,840, Visits: 3,971
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
Post #904873
Posted Friday, April 16, 2010 1:42 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, September 29, 2014 3:31 PM
Points: 161, Visits: 461
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'.
Post #905178
Posted Sunday, April 18, 2010 11:59 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 12:23 PM
Points: 35,403, Visits: 31,965
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #905706
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse