Solved

while loop in storedprocedure sql server 2005

Posted on 2009-07-05
6
987 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 14

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 14

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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 14

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 14

Expert Comment

by:rob_farley
ID: 24783220
Rahu,

Cursors are generally NOT the right answer.

Rob
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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

13 Experts available now in Live!

Get 1:1 Help Now