| Author |
Topic |
|
jim65
Starting Member
35 Posts |
Posted - 2006-02-15 : 13:26:18
|
| Declare @Frequency_ID int DECLARE @CNT INTDECLARE @RptReq_ID intDeclare @Startdate as smalldatetime Declare @counter as intselect @counter = count(RptReqID) from adReportingRequirementSET @CNT = 1while @CNT <@counterBeginSELECT @Frequency_ID= [FrequencyID],@RptReq_ID= [RptReqID], @Startdate =[StartDate]FROM adReportingRequirementwhere RptReqID = @CNTif @Frequency_ID = 1print @Startdate 'from 1'if @Frequency_ID = 2print @Startdate 'from 2'SET @CNT = @CNT + 1endthis code is to get the FrequencyID, and Startdate from the adReportingRequirement table.then into the while loop. if FrequencyID is 1, print start date. the problem is sometime print two times or three times for one record. result looks like4/20/20053/06/20053/06/20053/06/2005the 3/06/2005 is only from one record. why is printing three times? is this code problem or something else. |
|
|
JoeNak
Constraint Violating Yak Guru
292 Posts |
Posted - 2006-02-15 : 13:50:37
|
| Posting some sample data from your table would help. Incidentally I think your while loop should read <= not < otherwise you'll miss the last record. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2006-02-15 : 13:52:14
|
| Is RptReqID the primary key on adReportingRequirement table? If not, is it unique at least?Tara Kizeraka tduggan |
 |
|
|
JoeNak
Constraint Violating Yak Guru
292 Posts |
Posted - 2006-02-15 : 13:57:06
|
| This loop assumes that RptReqID is sequential. Check for gaps in your RptReqID, you probably need to restructure your loop. |
 |
|
|
jim65
Starting Member
35 Posts |
Posted - 2006-02-15 : 14:01:09
|
| sample date from the adReportingRequirement tableRptReqID EntityID RptReqTypeID FrequencyID Days startDate EndDate, InactiveDate3 17508 2 1 4 4/20/2005 12/31/2006 4 34084 10 3 7 12/31/2003 5 34084 7 1 6 3/06/2005 8 18753 4 3 6 5/16/2005 5/20/2005 |
 |
|
|
JoeNak
Constraint Violating Yak Guru
292 Posts |
Posted - 2006-02-15 : 14:08:24
|
| Your loop is based off an arbitrary int (@CNT). As you go through your loop and assign values to your variables based on RptReqID = @CNT. The problem is there are no records with RptReqID = 6 or 7 to change the values of the variables. The variables retain their previous value. That's why you're getting 3/06/2005 printing 3 times.Just restructure you loop... |
 |
|
|
jim65
Starting Member
35 Posts |
Posted - 2006-02-15 : 14:10:31
|
| yes, Thanks a lot. |
 |
|
|
jim65
Starting Member
35 Posts |
Posted - 2006-02-16 : 10:13:50
|
| I think that just take off where sentence, then will be finelikeDeclare @Frequency_ID int DECLARE @CNT INTDECLARE @RptReq_ID intDeclare @Startdate as smalldatetime Declare @counter as intselect @counter = count(RptReqID) from adReportingRequirementSET @CNT = 1while @CNT <@counterBeginSELECT @Frequency_ID= [FrequencyID],@RptReq_ID= [RptReqID], @Startdate =[StartDate]FROM adReportingRequirement--where RptReqID = @CNTif @Frequency_ID = 1print @Startdate 'from 1'if @Frequency_ID = 2print @Startdate 'from 2'SET @CNT = @CNT + 1end |
 |
|
|
JoeNak
Constraint Violating Yak Guru
292 Posts |
Posted - 2006-02-16 : 10:31:25
|
No it won't.quote: while @CNT <@counter
Your loop will run Number of records - 1 times quote: SELECT @Frequency_ID= [FrequencyID],@RptReq_ID= [RptReqID], @Startdate =[StartDate]FROM adReportingRequirement--where RptReqID = @CNT
This will assign values based on the last record returned to your variables every time.Try something like this....DECLARE @RowCnt INT, @FreqID INT, @RptReqID INT, @StartDate SMALLDATETIMESELECT TOP 1 @FreqID = FrequencyID, @RptReqID = RptReqID, @StartDate = StartDateFROM adReportingRequirementORDER BY RptReqIDSET @RowCnt = @@ROWCOUNTWHILE @RowCnt <> 0 BEGIN insert work done by loop SELECT TOP 1 @FreqID = FrequencyID, @RptReqID = RptReqID, @StartDate = StartDate FROM adReportingRequirement WHERE RptReqID > @RptReqID ORDER BY RptReqID SET @RowCnt = @@ROWCOUNT END |
 |
|
|
jim65
Starting Member
35 Posts |
Posted - 2006-02-16 : 11:13:59
|
| You are right,it works. many thanks |
 |
|
|
|