Here is a code we can generate in SQL to use artificial subroutine.
From any part of your code or stored procedure you can call standard set of commands (aka sub-stored procedure) and come back without actually calling another stored procedure or function.
USE Tempdb
GO
CREATE PROCEDURE #Test_Loop AS
DECLARE @TestText varchar(100);
DECLARE @i int;
DECLARE @RetCall TINYINT;
-- CALL #1
SELECT @RetCall = 1, @TestText = 'CALL
#1', @i = 10;
PRINT 'First
Subroutine call:'
GOTO PrintFunction;
CALL1:
-- CALL #2
SELECT @RetCall = 2, @TestText = 'CALL
#2', @i = 15;
PRINT 'Second
Subroutine call:'
GOTO PrintFunction;
CALL2:
GOTO EndCall;
-------------------------------------------------------PrintFunction:
WHILE @i > 0
BEGIN
PRINT CAST(@i as VARCHAR) + ' - ' +
@TestText
SET @i -= 1;
END
IF @RetCall = 1 GOTO CALL1;
IF @RetCall = 2 GOTO CALL2;
GOTO EndCall;
------------------------------------------------------
EndCall:
RETURN 0;
GO
EXEC #Test_Loop
GO
DROP PROCEDURE #Test_Loop
No comments:
Post a Comment