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)
 While Loop

Author  Topic 

sg2255551
Constraint Violating Yak Guru

274 Posts

Posted - 2010-09-27 : 11:39:13
hi

I have in my setdata field 1 2 3 4 5. when i ran this:

Declare @X as nvarchar(50), @loop int, @result as nvarchar(50)
set @loop = 0
Select @X = Count(setdata) from dbo.setdata

WHILE @loop < @X
BEGIN
select @result = setdata from dbo.setdata
print @result
SET @Loop = @Loop + 1
END


It print out 5 5 5 5 5. how do i make to printout 1 2 3 4 5? thanks

robvolk
Most Valuable Yak

15732 Posts

Posted - 2010-09-27 : 11:46:37
While would you want to do this when:

SELECT setdata FROM setdata

Works just fine?
Go to Top of Page

sg2255551
Constraint Violating Yak Guru

274 Posts

Posted - 2010-09-27 : 11:53:26
hi

I want to use the variable for some other purpose. Thanks
Go to Top of Page

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-09-27 : 12:06:06
quote:
Originally posted by sg2255551

hi

I have in my setdata field 1 2 3 4 5. when i ran this:

Declare @X as nvarchar(50), @loop int, @result as nvarchar(50)
set @loop = 0
Select @X = Count(setdata) from dbo.setdata

WHILE @loop < @X
BEGIN
select @result = setdata from dbo.setdata --There is no where condition for it to use @loop value. As a result it will assing one by one all values and the last value is getting stored in variable. print @result
SET @Loop = @Loop + 1
END


It print out 5 5 5 5 5. how do i make to printout 1 2 3 4 5? thanks



Regards,
Bohra

I am here to learn from Masters and help new bees in learning.
Go to Top of Page

sg2255551
Constraint Violating Yak Guru

274 Posts

Posted - 2010-09-27 : 12:12:18
hi

My apology, how should i write the where condition in the while loop? thanks
Go to Top of Page

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-09-27 : 12:22:48
quote:
Originally posted by sg2255551

hi

My apology, how should i write the where condition in the while loop? thanks



If we know your requirement clearly then we can give better suggestion.

With limited information given by you, I can only suggest:

select @result = setdata from dbo.setdata where setdata = @loop
print @result
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-09-27 : 13:13:37
do you remeber Kritey Alley from Cheers?


Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx





Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-09-28 : 11:35:55
Do numbering at your front end application, or make use of row_number() function
http://beyondrelational.com/blogs/madhivanan/archive/2007/08/27/multipurpose-row-number-function.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

sg2255551
Constraint Violating Yak Guru

274 Posts

Posted - 2010-09-29 : 01:47:32
hi

Thanks a lot. it is solved
Go to Top of Page
   

- Advertisement -