Sql Server中的GOTO语句 [英] GOTO Statement in Sql Server

查看:1181
本文介绍了Sql Server中的GOTO语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我对SqlServer中的GOTO语句有些怀疑。

  DECLARE   @ a   Int  
DECLARE @ b Int
SELECT @ a = 10
SELECT @ b = < span class =code-digit> 5
BEGIN
IF @ a> @b GOTO
IF @ a< @b GOTO 较小
END
更高: SELECT @ a
较小: SELECT @b





执行这些语句时,我会执行两个标签。我想只获得@a的价值。





以下我从MSDN获得的代码。

但是我不明白为什么Branch_Two:标签没有被执行......我很困惑....





  DECLARE   @ Counter   INT ; 
SET @ Counter = 1 ;
WHILE @ Counter < 10
BEGIN
SELECT @ Counter
SET @ Counter = @ Counter + 1
IF @ Counter = 3 GOTO Branch_One - 跳转到第一个分支。
IF @ Counter = 4 GOTO Branch_Two - 这将永远不会执行。
END
Branch_One:
SELECT ' 跳转到第一分支。'
GOTO Branch_Three; - 这将阻止Branch_Two执行。
Branch_Two:
SELECT ' 跳转到第二分支。'
Branch_Three:
SELECT ' 跳转到分支三。'







提前致谢

解决方案

阅读本文它会给你一些想法。

http://blog.sqlauthority.com/2007/05/07/sql-server-20052000-examples-and-explanation-for-goto/ [ ^ ]



在第一个代码片段中,v a的意思是> b所以 GOTO大这是执行的。

所以更高:SELECT @a 这将得到执行。之后执行下一行较小:SELECT @b

因为执行标签中的代码后没有返回的语句,较小的标签将被执行。



@Counter 变为3的第二个代码段中,

IF @Counter = 3 GOTO Branch_One 执行,光标退出while循环。

下的标签分支一个这两个陈述在那里



 SELECT'跳转到分支一个。'< br / > 
GOTO Branch_Three;< br />





在上面的代码中选择后, GOTO Branch_Three; 语句是这样的,因此游标将在完成label <$的执行后执行标签 branch_three

下的语句c $ c> branch_three

没有声明可以执行。

因为光标从while循环中出来 branch_Two 永远不会被执行。


我希望你理解。


检查下面



< pre lang =sql> DECLARE @ a Int
DECLARE @ b Int
SELECT @ a = 10
SELECT @ b = 5
BEGIN
IF @ a> @b GOTO
ELSE IF @ a< ; @b GOTO 较小
END
更高: SELECT @ a return
较小: SELECT @ b return


试试这个



  DECLARE   @ a   Int  
DECLARE @ b Int
SELECT @ a = 10
SELECT @ b = 5
BEGIN
IF @ a> @b GOTO
我F @ a< @b GOTO 较小的
END
更多: SELECT @ a
GOTO 最后;
较小: SELECT @ b
GOTO 最后;
最后一个:





这里Last作为休息; C#中的陈述

GOTO最后;没有添加它会执行较小的部分也



所以你得到两个输出

以上变更工作正常



让我知道你是否有任何干旱



你可以用kishorekke所做的或者在上面的脚本


Hi,

I've some doubts in GOTO Statement in SqlServer.

DECLARE	@a	Int
DECLARE	@b	Int
SELECT	@a	=	10
SELECT	@b	=	5
BEGIN
	IF		@a>@b GOTO	Greater
	IF		@a<@b GOTO	Lesser
END
Greater:	SELECT	@a
Lesser:		SELECT	@b



While executing these statements I get both labels executed. I suppose to get the value of "@a" only.


And below piece of code I got from MSDN.
But I don't understand why the "Branch_Two:" label doesn't get executed... I'm confused....


DECLARE @Counter int;
SET @Counter = 1;
WHILE @Counter < 10
BEGIN 
    SELECT @Counter
    SET @Counter = @Counter + 1
    IF @Counter = 3 GOTO Branch_One  --Jumps to the first branch.
    IF @Counter = 4 GOTO Branch_Two  --This will never execute.
END
Branch_One:
    SELECT 'Jumping To Branch One.'
    GOTO Branch_Three; --This will prevent Branch_Two from executing.
Branch_Two:
    SELECT 'Jumping To Branch Two.'
Branch_Three:
    SELECT 'Jumping To Branch Three.'




Thanks in advance

解决方案

Read this it will give you some idea.
http://blog.sqlauthority.com/2007/05/07/sql-server-20052000-examples-and-explanation-for-goto/[^]

In the first code snippet the value of a is > b so GOTO Greater this is executed.
so Greater: SELECT @a this will get executed. after that next line is executed which is Lesser: SELECT @b .
since there is no statement to return after executing the code in label greater, the label lesser will get executed.

In the second code snippet when @Counter becomes 3 then
IF @Counter = 3 GOTO Branch_One is executed and the cursor comes out of while loop.
under label branch one these 2 statements are there

SELECT 'Jumping To Branch One.'<br />
    GOTO Branch_Three;<br />



in the above code after select, GOTO Branch_Three; statement is there so cursor will execute the statements under label branch_three
after finishing the execution of label branch_three
no statement is there to execute.
as the cursor came out of while loop branch_Two will never get executed.

I hope you understood.


Check below

DECLARE @a  Int
DECLARE @b  Int
SELECT  @a  =   10
SELECT  @b  =   5
BEGIN
    IF      @a>@b GOTO  Greater
    ELSE IF     @a<@b GOTO  Lesser
END
Greater:    SELECT  @a  return
Lesser:     SELECT  @b return


Try this

DECLARE	@a	Int
DECLARE	@b	Int
SELECT	@a	=	10
SELECT	@b	=	5
BEGIN
	IF		@a>@b GOTO	Greater
	IF		@a<@b GOTO	Lesser
END
Greater:	SELECT	@a
			GOTO Last;
Lesser:		SELECT	@b
			GOTO Last;
Last:



Here Last works as break; statment in C#
GOTO Last; is not added it will execute the Lesser part also

so you are getting both the outputs
above change works fine

Let me know if you have any drought

You can do in both the ways what kishorekke has done or as in the above script


这篇关于Sql Server中的GOTO语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆