| 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 intdeclare @rmax intdeclare @r2go intdeclare @r2max intdeclare @str varchar(8000)declare @refarray char(8)declare @let char(1)declare @tname varchar(10)select @refarray = 'abcdefgh'select @rgo = 1select @rmax = 8select @r2go = 1select @r2max = len(@refarray)while @rgo <= @rmaxbegin 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 + 1endThe only output I get as this stands is:tab1a..tab1hWhy is the outer loop not executing? Shouldn't I be seeingtab1a/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 intdeclare @rmax intdeclare @r2go intdeclare @r2max intdeclare @str varchar(8000)declare @refarray char(8)declare @let char(1)declare @tname varchar(10)select @refarray = 'abcdefgh'select @rgo = 1select @rmax = 8select @r2go = 1select @r2max = len(@refarray)while @rgo <= @rmaxbegin 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 + 1endThe only output I get as this stands is:tab1a..tab1hWhy is the outer loop not executing? Shouldn't I be seeingtab1a/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. |
 |
|
|
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? |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2008-06-26 : 11:13:23
|
| while @rgo <= @rmaxbeginwhile @r2go <= @r2maxbeginselect @let = substring(@refarray,@r2go,1)select @tname = 'tab'+convert(varchar,@rgo)+@letprint(@tname) select @r2go = @r2go + 1end ---- hereselect @r2go = 1----select @rgo = @rgo + 1endGreetingsWebfred |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-06-26 : 11:13:41
|
| while @rgo <= @rmaxbeginwhile @r2go <= @r2maxbeginselect @let = substring(@refarray,@r2go,1)select @tname = 'tab'+convert(varchar,@rgo)+@letprint(@tname) select @r2go = @r2go + 1end select @rgo = @rgo + 1select @r2go = 1endWhat do you want to do?MadhivananFailing to plan is Planning to fail |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-06-26 : 11:14:09
|
Again MadhivananFailing to plan is Planning to fail |
 |
|
|
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) |
 |
|
|
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! |
 |
|
|
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 MadhivananFailing to plan is Planning to fail |
 |
|
|
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 intdeclare @rmax intdeclare @r2go intdeclare @r2max intdeclare @str varchar(8000)declare @refarray char(8)declare @let char(1)declare @tname varchar(10)select @refarray = 'abcdefgh'select @rgo = 1select @rmax = 8select @r2go = 1select @r2max = len(@refarray)while @rgo <= @rmaxbegin 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 + 1endThe only output I get as this stands is:tab1a..tab1hWhy is the outer loop not executing? Shouldn't I be seeingtab1a/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 |
 |
|
|
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 intdeclare @rmax intdeclare @r2go intdeclare @r2max intdeclare @str varchar(8000)declare @refarray char(8)declare @let char(1)declare @tname varchar(10)select @refarray = 'abcdefgh'select @rgo = 1select @rmax = 8select @r2go = 1select @r2max = len(@refarray)while @rgo <= @rmaxbegin 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 + 1endThe only output I get as this stands is:tab1a..tab1hWhy is the outer loop not executing? Shouldn't I be seeingtab1a/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 infohttp://sqlblogcasts.com/blogs/madhivanan/archive/2007/12/04/column-length-and-data-length.aspxMadhivananFailing to plan is Planning to fail |
 |
|
|
|