Take the 2-minute tour ×

is it good practise to use 'goto' statements in Sql queries?

share|improve this question
xkcd.com/292 –  ircmaxell Jun 15 '10 at 14:18
@ircmaxell my sentiments exactly. –  Meiscooldude Jun 15 '10 at 14:20

4 Answers 4

up vote 4 down vote accepted

Depends on the SQL - some of the dialects don't provide a useful mechanism for flow control other than GOTO.

GOTO is generally bad form.

share|improve this answer


As with other languages, there's almost always a better option to use than a Goto.

If you tell us which SQL Package you're using and what you're trying to accomplish, we might be able to give you an idea as to exactly which might fit.

share|improve this answer
It's a stored procedure written by someone. –  SoftwareGeek Jun 15 '10 at 15:38
@BhejaFry - But is it T-SQL, PL-SQL, MySQL, etc.? –  Justin Niessner Jun 15 '10 at 18:34
it's T-SQL, won't pl-sql support goto? –  SoftwareGeek Jun 16 '10 at 1:15

Not in production code but for testing could be ok.

For example, wanting to provide regression testing for a stored procedure where the "common bit" is the call to the procedure being tested and debug statements.

declare @test int;
set @test = 1;
goto tests

print "common bit"

if @test = 1 print "1";
if @test = 2 print "2";
if @test = 3 print "3";

set @test = @test + 1;
if @test <= 3 goto common
print "finished " + cast(@test as varchar(5))
go  -- goto can not be used past go!

Being a t-sql noob I was hoping for procedure or function to declare within scope to do the "common bit" but this was the best I could come up with after much googling. Why would you have to setup a stored procedure for every bit of code you want to re-use. Especially for non production work.

share|improve this answer
You make a great point about using small bits of functionality using GOTO. :-) We need Lambda expressions in T-SQL! ;-) –  Norman H Jul 26 '13 at 12:42

My guess would be no. My general rule with goto statements in any modern language is, if your using them, something is wrong with your design.

share|improve this answer

Your Answer


