Solved

while loop in storedprocedure sql server 2005

Posted on 2009-07-05
6
1,023 Views
Last Modified: 2012-06-27
hi

i want to write stored procedure to process each record returned from "select query".
i mean i want to use "while loop" in my stored procedure, to process each record. Could you please help
i am using sql server 2005.
0
Comment
Question by:ram27
[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
  • 4
6 Comments
 
LVL 15

Expert Comment

by:rob_farley
ID: 24782424
What you're describing is a cursor, but they're generally not the best way of processing data.

If you post what you need to do with each record, I'm sure there are plenty of people here who can help you avoid using a cursor (myself included).

Rob
0
 

Author Comment

by:ram27
ID: 24782432
select @StartTime=CP.StartTime from dbo.tblContact c
      inner join dbo.tblContactPeriod CP on C.ContactPeriodID = CP.ContactPeriodID
--  print @StartTime  
  -- print Datepart(hh,@StartTime)
   
-- here i need to check each @startTime which is returned in above sql select stmt
       
if((DATEPART(hh,@StartTime)>=21 and DATEPART(hh,@StartTime)<=24))
   Begin
     
          select CP.StartTime, C.ContactID, C.Product_VoicemailPhone, TMP.TextMessageEmail, C.Product_TextMessagePhone, C.Product_Email
          from dbo.tblContact C
              inner join dbo.tblContactPeriod CP on C.ContactPeriodID = CP.ContactPeriodID
             inner join dbo.tblColorDrawn CD on CD.ColorID=C.ColorID
              left outer join dbo.tblTextMessageProvider TMP on TMP.ProviderID = C.Product_TextMessageProviderID   --outer join because not everyone selects Text Messages.
         where DATEPART(hh,dbo.ConvertToGmt(CP.StartTime, C.Timezone, C.DST)) = DATEPART(hh,@GmtServer)
              and IsActive = 1
              and @CurrentTime >= StartDateService
              and CD.DateDrawn=DateADD(d,1,@CurrentTime)
             and EndDateService IS NULL
       
     
         
    End
Else
   
    Begin
   
       select CP.StartTime, C.ContactID, C.Product_VoicemailPhone, TMP.TextMessageEmail, C.Product_TextMessagePhone, C.Product_Email
       from dbo.tblContact C
              inner join dbo.tblContactPeriod CP on C.ContactPeriodID = CP.ContactPeriodID
             inner join dbo.tblColorDrawn CD on CD.ColorID=C.ColorID
              left outer join dbo.tblTextMessageProvider TMP on TMP.ProviderID = C.Product_TextMessageProviderID   --outer join because not everyone selects Text Messages.
       where DATEPART(hh,dbo.ConvertToGmt(CP.StartTime, C.Timezone, C.DST)) = DATEPART(hh,@GmtServer)
              and IsActive = 1
              and @CurrentTime >= StartDateService
              and CD.DateDrawn=@CurrentTime
              and EndDateService IS NULL
       End
     
     
0
 
LVL 15

Expert Comment

by:rob_farley
ID: 24782451
Ok, so because the WHERE clause changes, you feel like you want to run each contactperiod separately.

So try:

and
(
DATEPART(hh,CP.StartTime)>=21 and DATEPART(hh,CP.StartTime)<=24 and CD.DateDrawn=DateADD(d,1,@CurrentTime)
or
NOT (DATEPART(hh,CP.StartTime)>=21 and DATEPART(hh,CP.StartTime)<=24) and CD.DateDrawn=@CurrentTime
)

instead of that line that changes (the "and CD.DateDrawn=@CurrentTime" line)

Rob
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Accepted Solution

by:
rob_farley earned 500 total points
ID: 24782455
ie:


select CP.StartTime, C.ContactID, C.Product_VoicemailPhone, TMP.TextMessageEmail, C.Product_TextMessagePhone, C.Product_Email
       from dbo.tblContact C
              inner join dbo.tblContactPeriod CP on C.ContactPeriodID = CP.ContactPeriodID
             inner join dbo.tblColorDrawn CD on CD.ColorID=C.ColorID
              left outer join dbo.tblTextMessageProvider TMP on TMP.ProviderID = C.Product_TextMessageProviderID   --outer join because not everyone selects Text Messages.
       where DATEPART(hh,dbo.ConvertToGmt(CP.StartTime, C.Timezone, C.DST)) = DATEPART(hh,@GmtServer)
              and IsActive = 1
              and @CurrentTime >= StartDateService
              and
              (
              DATEPART(hh,CP.StartTime)>=21 and DATEPART(hh,CP.StartTime)<=24 and CD.DateDrawn=DateADD(d,1,@CurrentTime)
              or
              NOT (DATEPART(hh,CP.StartTime)>=21 and DATEPART(hh,CP.StartTime)<=24) and CD.DateDrawn=@CurrentTime
              )
              and EndDateService IS NULL
0
 
LVL 9

Expert Comment

by:Rahul Goel ITIL
ID: 24783217
Use Cursors to process each record accordingly.
Example
http://www.mssqltips.com/tip.asp?tip=1599
0
 
LVL 15

Expert Comment

by:rob_farley
ID: 24783220
Rahu,

Cursors are generally NOT the right answer.

Rob
0

Featured Post

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

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

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

623 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