SQL Server转到游标来自的下一行

时间:2019-01-12 14:05:59

标签: sql-server goto

我需要使用goto从发送光标的那一行返回。目的是多次使用存储过程中的零件打印结果

declare @a int, @b int

set @a = 1
set @b = 1

nextb:
    goto printresult
    set @b = @b + 1
    if @b < 10 goto nextb

lessb:
    goto printresult
    set @b = @b - 1
    if @b > 1 goto nextb
    goto finish

printresult:    
    select @a, @b 
    --goto ? need to go back the next line where the cursor was sent from

finish:
    select 'finish'

1 个答案:

答案 0 :(得分:0)

如果我了解您对相关代码的期望可以为您提供帮助:

/*** Effacement: ********************************************************
IF EXISTS ( SELECT name FROM sysobjects
            WHERE type = 'V' AND name = 'procPrintresult' )
    DROP PROCEDURE procPrintresult
*** Effacement: ********************************************************/

CREATE PROCEDURE procPrintresult 
    @a int, @b int
AS
SET NOCOUNT ON
    SELECT  @a AS 'A', @b AS 'B'

SET NOCOUNT OFF
GO


/*** Effacement: ********************************************************
IF EXISTS ( SELECT name FROM sysobjects
            WHERE type = 'V' AND name = 'procExecutionCodeWanted' )
    DROP PROCEDURE procExecutionCodeWanted
*** Effacement: ********************************************************/

CREATE PROCEDURE procExecutionCodeWanted 
AS
SET NOCOUNT ON
    DECLARE @a int = 1
    DECLARE @b int = 1

    -- nextb block
    while @b < 10 
    begin
        exec procPrintresult @a, @b
        SET @b = @b +1
    end


    -- lessb block
    while @b > 1
    begin 
        exec procPrintresult @a, @b
        SET @b = @b - 1
    end

    -- finish storedProc
    SELECT  'finish' AS 'Finish'

SET NOCOUNT OFF 
GO


EXEC procExecutionCodeWanted