Solved

convert to SQL Server loop

Posted on 2008-10-09
4
204 Views
Last Modified: 2010-03-19
Hi,
I have this code which has to be written in SQL Server stored Proc . Basically stored proc accepts
StartDate and EndDate and Calcuates the days and loops thru the end of the date and runs the select statement .
   S = DateDiff("d", varStartDate, varEndDate - 1)
                If s > 0 Then
                    For i = 1 To s
                                          
                      Select * from tblBank where bankID = 1 and date = dateAdd("d",s,varstartdate)
                      and bankID = varbankID
0
Comment
Question by:dotnet0824
  • 3
4 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22683359
First let me suggest, you just do this with a normal query, but I will post a loop shortly:
(seems like you are adding a day at beginning and end, so you technically can just input startdate one day later and end date one day sooner -- but coded per loop above)
Select * 

from tblBank 

where bankID = @varbankID

and date between (@varstartdate+1) and (@varenddate-1)

-- and bankID = 1 /* commented this out as already filtering bankID */

Open in new window

0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 22683368
Here is loop:
WHILE (@varstartdate+1) <= (@varenddate-1)

BEGIN
 

Select * 

from tblBank 

where bankID = @varbankID

and date = (@varstartdate+1)
 

SET @varstartdate = @varstartdate+1
 

END

Open in new window

0
 

Author Comment

by:dotnet0824
ID: 22685522
thanks a lot mwvisa.. I posted a query to add records to temp table if you could help.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22686042
You are welcome.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now