Solved

convert to SQL Server loop

Posted on 2008-10-09
4
205 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Simple SQL query from two tables 13 55
CPU high usage when update statistics 2 30
Tsql query 6 22
performance query 4 24
I have a large data set and a SSIS package. How can I load this file in multi threading?
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

831 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