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 2005 Forums
 Transact-SQL (2005)
 Trouble with a while loop

Author  Topic 

KidSQL
Yak Posting Veteran

88 Posts

Posted - 2008-06-26 : 11:04:17
Hello, I have the following nested loop (yes I know it's not the set way, but it does what it does in a fraction of a second so it's okay):

declare @rgo int
declare @rmax int
declare @r2go int
declare @r2max int
declare @str varchar(8000)
declare @refarray char(8)
declare @let char(1)
declare @tname varchar(10)

select @refarray = 'abcdefgh'
select @rgo = 1
select @rmax = 8
select @r2go = 1
select @r2max = len(@refarray)



while @rgo <= @rmax
begin
while @r2go <= @r2max
begin
select @let = substring(@refarray,@r2go,1)
select @tname = 'tab'+convert(varchar,@rgo)+@let
print(@tname)
select @r2go = @r2go + 1
end
select @rgo = @rgo + 1
end


The only output I get as this stands is:

tab1a..tab1h

Why is the outer loop not executing? Shouldn't I be seeing

tab1a/b/c/d/e/f/g/h through tab8a/b/c/d/e/f/g/h?

Is there something I misunderstand about while loops or just something that's missed my eye? (If the latter, apologies in advance, but I would appreciate any help).

Any advice is much appreciated.

Thanks,

-KS

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-26 : 11:07:30
quote:
Originally posted by KidSQL

Hello, I have the following nested loop (yes I know it's not the set way, but it does what it does in a fraction of a second so it's okay):

declare @rgo int
declare @rmax int
declare @r2go int
declare @r2max int
declare @str varchar(8000)
declare @refarray char(8)
declare @let char(1)
declare @tname varchar(10)

select @refarray = 'abcdefgh'
select @rgo = 1
select @rmax = 8
select @r2go = 1
select @r2max = len(@refarray)



while @rgo <= @rmax
begin
while @r2go <= @r2max
begin
select @let = substring(@refarray,@r2go,1)
select @tname = 'tab'+convert(varchar(length),@rgo)+@let
print(@tname)
select @r2go = @r2go + 1
end
select @rgo = @rgo + 1
end


The only output I get as this stands is:

tab1a..tab1h

Why is the outer loop not executing? Shouldn't I be seeing

tab1a/b/c/d/e/f/g/h through tab8a/b/c/d/e/f/g/h?

Is there something I misunderstand about while loops or just something that's missed my eye? (If the latter, apologies in advance, but I would appreciate any help).

Any advice is much appreciated.

Thanks,

-KS


You've not specified length of varchar field while casting. so it will take the default value of 1.
Go to Top of Page

KidSQL
Yak Posting Veteran

88 Posts

Posted - 2008-06-26 : 11:09:34
Thanks for the reply. However, when I change this to any length (like: select @tname = 'tab'+convert(varchar(32),@rgo)+@let)

I still get the same output. Any ideas?
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2008-06-26 : 11:13:23
while @rgo <= @rmax
begin
while @r2go <= @r2max
begin
select @let = substring(@refarray,@r2go,1)
select @tname = 'tab'+convert(varchar,@rgo)+@let
print(@tname)
select @r2go = @r2go + 1
end
---- here
select @r2go = 1
----
select @rgo = @rgo + 1
end

Greetings
Webfred
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-06-26 : 11:13:41

while @rgo <= @rmax
begin
while @r2go <= @r2max
begin
select @let = substring(@refarray,@r2go,1)
select @tname = 'tab'+convert(varchar,@rgo)+@let
print(@tname)
select @r2go = @r2go + 1
end
select @rgo = @rgo + 1
select @r2go = 1
end


What do you want to do?

Madhivanan

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

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-06-26 : 11:14:09
Again

Madhivanan

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

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2008-06-26 : 11:16:38
i hope that gun has loaded only rubber bullets ;o)
Go to Top of Page

KidSQL
Yak Posting Veteran

88 Posts

Posted - 2008-06-26 : 11:20:51
Thanks madhivanan!! I just needed an extra pair of eyes.

By the way, if I may say so, I remember joining this forum not long after you did, back when you only had a hundred or so posts to your name. I must say you've come a long way - impressive for the rest of us.

Thanks again!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-06-26 : 13:31:14
quote:
Originally posted by KidSQL

Thanks madhivanan!! I just needed an extra pair of eyes.

By the way, if I may say so, I remember joining this forum not long after you did, back when you only had a hundred or so posts to your name. I must say you've come a long way - impressive for the rest of us.

Thanks again!


Thanks. You are welcome

Madhivanan

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

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2008-06-27 : 05:26:50
Know it's kind of a hijack after the problems fixed but if you don't specify a length for VARCHAR in a CAST it will set to whatever the DEFAULT length for a VARCHAR is in your database - not necessarily a length of 1.

It's probably a good idea not to expect a CAST like that to work the same on different databases!

Charlie.

quote:
Originally posted by visakh16

quote:
Originally posted by KidSQL

Hello, I have the following nested loop (yes I know it's not the set way, but it does what it does in a fraction of a second so it's okay):

declare @rgo int
declare @rmax int
declare @r2go int
declare @r2max int
declare @str varchar(8000)
declare @refarray char(8)
declare @let char(1)
declare @tname varchar(10)

select @refarray = 'abcdefgh'
select @rgo = 1
select @rmax = 8
select @r2go = 1
select @r2max = len(@refarray)



while @rgo <= @rmax
begin
while @r2go <= @r2max
begin
select @let = substring(@refarray,@r2go,1)
select @tname = 'tab'+convert(varchar(length),@rgo)+@let
print(@tname)
select @r2go = @r2go + 1
end
select @rgo = @rgo + 1
end


The only output I get as this stands is:

tab1a..tab1h

Why is the outer loop not executing? Shouldn't I be seeing

tab1a/b/c/d/e/f/g/h through tab8a/b/c/d/e/f/g/h?

Is there something I misunderstand about while loops or just something that's missed my eye? (If the latter, apologies in advance, but I would appreciate any help).

Any advice is much appreciated.

Thanks,

-KS


You've not specified length of varchar field while casting. so it will take the default value of 1.



-------------
Charlie
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-06-27 : 09:40:04
quote:
Originally posted by Transact Charlie

Know it's kind of a hijack after the problems fixed but if you don't specify a length for VARCHAR in a CAST it will set to whatever the DEFAULT length for a VARCHAR is in your database - not necessarily a length of 1.

It's probably a good idea not to expect a CAST like that to work the same on different databases!

Charlie.

quote:
Originally posted by visakh16

quote:
Originally posted by KidSQL

Hello, I have the following nested loop (yes I know it's not the set way, but it does what it does in a fraction of a second so it's okay):

declare @rgo int
declare @rmax int
declare @r2go int
declare @r2max int
declare @str varchar(8000)
declare @refarray char(8)
declare @let char(1)
declare @tname varchar(10)

select @refarray = 'abcdefgh'
select @rgo = 1
select @rmax = 8
select @r2go = 1
select @r2max = len(@refarray)



while @rgo <= @rmax
begin
while @r2go <= @r2max
begin
select @let = substring(@refarray,@r2go,1)
select @tname = 'tab'+convert(varchar(length),@rgo)+@let
print(@tname)
select @r2go = @r2go + 1
end
select @rgo = @rgo + 1
end


The only output I get as this stands is:

tab1a..tab1h

Why is the outer loop not executing? Shouldn't I be seeing

tab1a/b/c/d/e/f/g/h through tab8a/b/c/d/e/f/g/h?

Is there something I misunderstand about while loops or just something that's missed my eye? (If the latter, apologies in advance, but I would appreciate any help).

Any advice is much appreciated.

Thanks,

-KS


You've not specified length of varchar field while casting. so it will take the default value of 1.



-------------
Charlie


More info
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/12/04/column-length-and-data-length.aspx

Madhivanan

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

- Advertisement -