Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Strange while Loop Behaviour

Author  Topic 

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2013-03-10 : 08:50:17
Hi All

I am finding very strange behaviour inside while ...I have big stored procedure which do the calculation on business requirement . In my sp aim using while loop and its resulting duplicates.. I can't paste that proc here because it doing number of things that i will not be able to explain.. but i created same scenario to explain that issue.


declare @min int,@max int,@datarow varchar(2000),@id int
create table #table ( id int, datarow varchar(2000))

set @id= ''
set @datarow=''
insert into #table(id,datarow)
select 1,'hetejajla'
union all
select 4,'hetejajla'

Select * from #table

Select @min=MIN(id) ,@max =MAX(id) from #table
--- Select @min, @max
while (@min<=@max)
begin

Select @id=id,@datarow=datarow from #table where id =@min
Select @id as id ,@datarow as datarow
set @min=@min+1

--select
end

you can see that in my table #table has only two records with ids 1 and 4.
When I run above code which gives me result .

id datarow
1 hetejajla

id datarow
1 hetejajla

id datarow
1 hetejajla


id datarow
4 hetejajla

because of this 2 records converted on 4 records ...This loop shold only display two entries


id datarow
1 hetejajla

id datarow
4 hetejajla


Why this is showing extra records...Pleas suggest me on this tried every possible stuff but can't get rid of the this behaviour.






Vijay is here to learn something from you guys.

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2013-03-10 : 09:30:58
quote:
Originally posted by vijays3

Hi All

I am finding very strange behaviour inside while ...I have big stored procedure which do the calculation on business requirement . In my sp aim using while loop and its resulting duplicates.. I can't paste that proc here because it doing number of things that i will not be able to explain.. but i created same scenario to explain that issue.


declare @min int,@max int,@datarow varchar(2000),@id int
create table #table ( id int, datarow varchar(2000))

set @id= ''
set @datarow=''
insert into #table(id,datarow)
select 1,'hetejajla'
union all
select 4,'hetejajla'

Select * from #table

Select @min=MIN(id) ,@max =MAX(id) from #table
--- Select @min, @max
while (@min<=@max)
begin
if exists(select * from #table where id = @min)
begin

Select @id=id,@datarow=datarow from #table where id =@min
Select @id as id ,@datarow as datarow
end
set @min=@min+1

--select
end

you can see that in my table #table has only two records with ids 1 and 4.
When I run above code which gives me result .

id datarow
1 hetejajla

id datarow
1 hetejajla

id datarow
1 hetejajla


id datarow
4 hetejajla

because of this 2 records converted on 4 records ...This loop shold only display two entries


id datarow
1 hetejajla

id datarow
4 hetejajla


Why this is showing extra records...Pleas suggest me on this tried every possible stuff but can't get rid of the this behaviour.






Vijay is here to learn something from you guys.




Too old to Rock'n'Roll too young to die.
Go to Top of Page

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2013-03-10 : 10:23:34
Thanks webfred it is working..

But why this behaviour happening...

Vijay is here to learn something from you guys.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2013-03-10 : 10:33:13
The selects with id 2 and 3 aren't returning anything so the vars @id and @datarow are not changed hence the "old" values are still there.


Too old to Rock'n'Roll too young to die.
Go to Top of Page
   

- Advertisement -