Solved

while loop in storedprocedure sql server 2005

Posted on 2009-07-05
6
995 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
  • 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
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.

 
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
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQl server restarts itself 6 32
Need help creating a stored procedure 4 56
SQL Trigger selecting another database 4 33
Analysis of table use 7 41
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

919 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

21 Experts available now in Live!

Get 1:1 Help Now