Solved

while loop in storedprocedure sql server 2005

Posted on 2009-07-05
6
1,011 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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