SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Strange while Loop Behaviour
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

vijays3
Constraint Violating Yak Guru

India
338 Posts

Posted - 03/10/2013 :  08:50:17  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

Germany
8679 Posts

Posted - 03/10/2013 :  09:30:58  Show Profile  Visit webfred's Homepage  Reply with Quote
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

India
338 Posts

Posted - 03/10/2013 :  10:23:34  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

Germany
8679 Posts

Posted - 03/10/2013 :  10:33:13  Show Profile  Visit webfred's Homepage  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000