Solved

convert to SQL Server loop

Posted on 2008-10-09
4
208 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 60

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 60

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 60

Expert Comment

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

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
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.

690 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