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 2000 Forums
 SQL Server Development (2000)
 Command to stop reading in code

Author  Topic 

pug2694328
Posting Yak Master

166 Posts

Posted - 2006-02-06 : 17:39:45
Is there a command that will tell the compiler to stop reading in code at a specific point? Currently the only way I know is to comment out code using scan comments.

Pseudocode example of what I'm looking for:
/* THIS QUERY WILL BE RUN */
SELECT
STUFF
FROM
TABLENAME

ENDSQL -- SOME COMMAND THAT SAYS STOP HERE AND SEND TO COMPILER

/* THIS WILL NOT BE READ IN, AND YET IS UNCOMMENTED */
SELECT
MORESTUFF
FROM
TABLENAME2

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-02-06 : 17:45:21
Option 1: If this is in a stored procedure, then you can use RETURN to exit it without processing the rest of the code.

Option 2: You could use a GOTO.

SELECT STUFF FROM TABLENAME

GOTO BailOut

SELECT MORESTUFF FROM TABLENAME2

BailOut:

Tara Kizer
aka tduggan
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2006-02-06 : 17:49:15
Putting in a go will cause the first lump to be executed before reading the rest.
a return will stop processing but not stop it being parsed so

SELECT
STUFF
FROM
TABLENAME
go
return
SELECT
MORESTUFF
FROM
TABLENAME2

will execute the first query but not the second. You will still get any errors from the second that are detected in parsing though.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

MichaelP
Jedi Yak

2489 Posts

Posted - 2006-02-06 : 17:55:15
You may want to look into IF..ELSE as well depending on exactly what you are trying to accomplish.

Michael

<Yoda>Use the Search page you must. Find the answer you will. Cursors, path to the Dark Side they are. Avoid them, you must. Use Order By NewID() to get a random record you will.</Yoda>
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-02-06 : 17:58:30
quote:
Originally posted by nr

Putting in a go will cause the first lump to be executed before reading the rest.



You referring to my post? If so, I mentioned GOTO.

Tara Kizer
aka tduggan
Go to Top of Page

pug2694328
Posting Yak Master

166 Posts

Posted - 2006-02-07 : 17:24:41
Thanks guys this gives me several options.
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2006-02-07 : 20:04:54
quote:
Originally posted by tkizer

quote:
Originally posted by nr

Putting in a go will cause the first lump to be executed before reading the rest.



You referring to my post? If so, I mentioned GOTO.

Tara Kizer
aka tduggan



No - I was exlaining what go does.
A goto won't allow the code to execute without parsing the rwmainder so didn't answer the question - but may be what was wanted.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-02-08 : 02:23:07
quote:
Originally posted by nr

Putting in a go will cause the first lump to be executed before reading the rest.
a return will stop processing but not stop it being parsed so

SELECT
STUFF
FROM
TABLENAME
go
return
SELECT
MORESTUFF
FROM
TABLENAME2

will execute the first query but not the second. You will still get any errors from the second that are detected in parsing though.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.


Nigel, that works without Go also.
The second part is parsed for systax errors but doesnt alert or display error for invalid objects. Does this mean that checking existance of objects is completely ignored?

Madhivanan

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

pug2694328
Posting Yak Master

166 Posts

Posted - 2006-02-08 : 08:17:31
I tried the GO and also GOTO Q where Q: is after all logic.
GO seems to still run the subsequent logic GOTO worked fine. I'll just put Q: at the end of every query and use it as needed.

Thanks so much for all your help!
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2006-02-08 : 15:44:28
>> Does this mean that checking existance of objects is completely ignored?
Objects are resolved at run time. If tables exist then the column names will be checked but if they don't it will not give an error until it executes. Putting the go in will get round syntax errors which prevent the batch starting.

Thinj this was a change made for v7 and is really annoying. It means that you don't know if there is a problem with a name in a stored proc until it runs - also means you don't get an error if you accidentally create it in the wrong database.
If I am on a critical system I'll often include an excecute of the sp with the creation script.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-02-09 : 02:31:41
Thanks Nigel.

Madhivanan

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

nr
SQLTeam MVY

12543 Posts

Posted - 2006-02-09 : 14:42:25
quote:
Originally posted by pug2694328

I tried the GO and also GOTO Q where Q: is after all logic.
GO seems to still run the subsequent logic GOTO worked fine. I'll just put Q: at the end of every query and use it as needed.

Thanks so much for all your help!



Have a look at the code I posted and the explanation.
You need a go followed by a erturn.
The go will cause the previous batch to be executed, the return will terminate execution.

Without a go a following syntax error will stop the first batch being executed, without the return the folowing batches will be executed.
A go followed by a return will cause the first batch to be executed only - you might get a syntax error frmo the following code but it will not be executed.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

pug2694328
Posting Yak Master

166 Posts

Posted - 2006-02-13 : 11:43:35
thanks nr
Go to Top of Page
   

- Advertisement -