Sql - goto语句

Sof*_*eek 12 t-sql sql-server goto

在SQL查询中使用'goto'语句是一种好习惯吗?

Nat*_*ate 9

取决于SQL - 一些方言不提供除GOTO之外的流控制的有用机制.

GOTO通常是糟糕的形式.


Gar*_*ann 8

不是生产代码,但测试可以.

例如,想要为存储过程提供回归测试,其中"公共位"是对正在测试的过程和调试语句的调用.

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

common:
print "common bit"

tests:
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!
Run Code Online (Sandbox Code Playgroud)

作为一个t-sql noob我希望程序或函数在范围内声明做"公共位",但这是我在google搜索后能想到的最好的.为什么要为要重用的每一段代码设置存储过程.特别是对于非生产工作.