July 30, 2003 at 9:09 am
my code is this:
while @counter<=60
begin
select @dateS = substring(datex,1,2)+substring(datex,3,2)+substring(datex,5,2)+'00'+'.'+str(@v1,2)+'S' from dates
select @path='dir e:\smdr\'+@dates
exec @result = master..xp_cmdshell @path, no_output
if (@result = 0)
PRINT 'File exist'
else
PRINT 'File Do NOT exist'
select @counter=@counter+1
select @v1=@v1+1
Print @path
what I need to do is :
if the file exists break the while and get off there....any ideas ??????
thanks
July 30, 2003 at 9:23 am
You could try the GOTO command like so:
declare @i int
set @i = 0
while @i < 60
begin
set @i = @i + 1
print @i
if @i > 9
goto while_exit
end
while_exit:
Print 'exited while statement'
Gregory Larsen, DBA
If you looking for SQL Server Examples check out my website at http://www.geocities.com/sqlserverexamples
Gregory A. Larsen, MVP
July 30, 2003 at 9:31 am
THANK YOU MY FRIEND 🙂 works
July 30, 2003 at 9:35 am
quote:
You could try the GOTO command like so:
just out of curiosity
Is GOTO a good practice in SQL Server?
Just asking, because every C programmer I've met, would tear off my head when I use goto. In VB it seems only be good practice in On Error blabla statements.
Couldn't I use BREAK ?
Cheers,
Frank
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
July 30, 2003 at 9:43 am
you could also use BREAK
declare @i int
set @i = 0
while @i < 60
begin
set @i = @i + 1
print @i
if @i > 9
begin
Print 'exited while statement'
break
end
end
July 30, 2003 at 9:43 am
I agree with frank, use break instead !
Paul
July 30, 2003 at 9:50 am
I should have known there would be a debate over the GOTO. Well I suppose a GOTO is considered bad if it makes the logic of your code hard to follow and you jump all around. Now the if a GOTO is used to logically drop the the bottom the bottom of a code block then I've always felt that was fine, since the processing follows a logically top to bottom flow.
Now the BREAK would also work, and might be considered better programming if you are really hung up on using the GOTO.
Gregory Larsen, DBA
If you looking for SQL Server Examples check out my website at http://www.geocities.com/sqlserverexamples
Gregory A. Larsen, MVP
July 30, 2003 at 9:51 am
I think GOTO is frowned upon as it makes understanding the code difficult...
However BREAK only breaks out of the innermost WHILE loop so GOTO could be used to break out of a deeply nested WHILE...
July 30, 2003 at 4:01 pm
GOTO is a good choice where Greg Larsen wants to use it.
I do find it funny that VB guys are complaining about GOTO. I've seen VB code that created contortions all on its own.
The GOTO debate is, what, 20 years old? I remember reading articles on it back in the 80's. Yes, it can cause headaches.
I have been a C programmer and there are places in code where GOTO makes the most sense.
Dr. Peter Venkman: Generally you don't see that kind of behavior in a major appliance.
Patrick
Quand on parle du loup, on en voit la queue
July 31, 2003 at 4:43 am
Hi Greg,
quote:
I should have known there would be a debate over the GOTO. Well I suppose a GOTO is considered bad if it makes the logic of your code hard to follow and you jump all around. Now the if a GOTO is used to logically drop the the bottom the bottom of a code block then I've always felt that was fine, since the processing follows a logically top to bottom flow.
don't get me wrong!
I don't really want to argue whether this or that is better, but I've been somewhat indoctrinated to stay away from GOTO whereever I can. Until your post I wasn't even aware that such a construct as GOTO is available in T-SQL.
In my view the best code (or programming style) is the one that suits you most.
You know, kind of live and let live philosophy.
And as for Patrick:
Good to see you on the forum again!
Have you yet been to France as you've announced a while ago?
BTW, I've never experienced a situation where you have to use GOTO in C. But that's another question.
Sorry for editing, but I wasn't sure about the wording!
Cheers,
Frank
Edited by - a5xo3z1 on 07/31/2003 06:09:58 AM
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
July 31, 2003 at 5:27 am
In this situation either BREAK or GOTO will work.
BREAK will simple stop the LOOP it is in and continue processing beyond that loop so it is inline processing.
GOTO however requires an label (anchor point) to jump to and is usually reserved for conditional logic to handle an unexpected event since it goes to a particular place in code.
However if you don't need to continue with any other kind of process in the code you are in then I would suggest RETURN, this will stop process immediately of the code and allows you to pass back an interger status so that you can tell the calling process why it ended.
July 31, 2003 at 5:40 am
use a bit variable for your break-condition and add it to your while:
@do_break = 0
while @counter < @upper_limit and @do_break = 0 begin
...
if [your_condition]
@do_break = 1
...
end
best regards,
chris.
July 31, 2003 at 6:36 am
Frank,
Thanks. No still in the US. The French trip is being set back to Octobre or Novembre.
I vaguely remember using GOTO a few times, but that was in the early 90's and I hope no one is running that program anymore. That would be scary.
Dr. Peter Venkman: Generally you don't see that kind of behavior in a major appliance.
Patrick
Quand on parle du loup, on en voit la queue
July 31, 2003 at 8:17 am
It's rather interesting that this topic has come up. I had adjusted an old proc the other day and put it through the release process here. The DBAs are now installing code through a system that first checks it for "issues". One that popped up in this script was a warning that "GOTO" statements are not suggested.
Personally, I have used GOTO's from time-to-time as I think it can actually make code more readable. I could easily take "proper" code and make it unreadable.
I think the best way to learn how to write readable code is to have to work on someone else's.
July 31, 2003 at 8:27 am
quote:
I think the best way to learn how to write readable code is to have to work on someone else's.
be careful with such statements!
There were times when I wished I'd meet the developer alone at night
Anyway, the right time for THE classic link on this topic
http://mindprod.com/unmain.html
Cheers,
Frank
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
Viewing 15 posts - 1 through 15 (of 19 total)
You must be logged in to reply to this topic. Login to reply