Friday, February 7, 2014

Ancient programming technics using GOTO

While I was improving my Documenter, I've been thinking about simplification and code re-usability without using functions or stored procedures and remembered old good days of programming and usage of GOTO command.

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