Quantcast
Channel: CodeSection,代码区,SQL Server(mssql)数据库 技术分享 - CodeSec
Viewing all articles
Browse latest Browse all 3160

GOTO Labels in SQL Server (And the answer to the FizzBuzz interview question)

$
0
0

Hello World!

So I was playing around at work today and decided for whatever reason to see how I could get the code I was writing to fire off only in certain situations.

If it’s Sunday maybe, or if this is in a particular environment, or if a record in an admin table was something specific. I’m not sure how I’ll use this but I stumbled on Labels and decided to play with them.

Ok, so how to get to know labels. Well, in order to get them to work sometimes I have to create labels that are based on some criteria.

Ok, sure. Let’s go with is an integer even or odd. Great, how do I determine if an integer is even or odd?

Turns out I learned another something as a byproduct of trying to learn about labels! @x % 2 checks to see if the variable x is divisible by 2?

DECLARE @i INT DECLARE @msg NVARCHAR(500) SET @i = 0 WHILE @i <= 100 BEGIN <strong>CheckValue:</strong> --First label! SET @msg = '' IF @i = 100 <strong>GOTO TheEnd</strong> IF @i%2 = 0 GOTO IsEven IF @i%2 = 1 GOTO IsOdd IsOdd: --SECOND LABEL!!! SET @msg = CAST(@i AS NVARCHAR(3)) + ' is Odd' PRINT @msg GOTO Increment IsEven: SET @msg = CAST(@i AS NVARCHAR(3)) + ' is Even' PRINT @msg GOTO Increment TheEnd: SET @msg = CAST(@i AS NVARCHAR(3)) + ' is the end' PRINT @msg GOTO Increment Increment: SET @i = @i+1 GOTO CheckValue END

I ended up having a ton of fun adding in checks for Multiples of 3, and 5, as well as altering the printed messages.

Lessons Learned:

GOTO Zoolander will skip to the Zoolander: label

If X%2 = 0 will check to see if X is a multiple of 2 (0 is yes, 1 is no)

Ultimately I ended up with this:

DECLARE @i int declare @msg nvarchar(500) set @i = 1 CheckValue: set @msg = '' if @i > 100 goto Done if @i%5 = 0 goto IsFive if @i%3 = 0 goto IsThree if @i%2 = 0 goto IsEven if @i = 1 goto IsOdd IsOdd: if @msg = '' set @msg = cast(@i as nvarchar(3)) + ' is Odd' else set @msg = @msg + ' and is Odd' print @msg set @i = @i+1 GoTo CheckValue IsEven: if @msg = '' set @msg = cast(@i as nvarchar(3)) + ' is Even' else set @msg = @msg + ' and is Even' print @msg set @i = @i+1 GoTo CheckValue IsThree: if @msg = '' set @msg = cast(@i as nvarchar(3)) + ' is a multiple of 3' else set @msg = @msg + ' and is a multiple of 3' if @i%2 = 1 GOTO IsOdd if @i%2 = 0 GOTO IsEven print @msg set @i = @i+1 GoTo CheckValue IsFive: if @msg = '' set @msg = cast(@i as nvarchar(3)) + ' is a multiple of 5' else set @msg = ' and is a multiple of 5' --print @msg if @i%3 = 0 goto IsThree if @i%2 = 1 GOTO IsOdd if @i%2 = 0 GOTO IsEven print @msg set @i = @i+1 GoTo CheckValue Done: Print 'End';

I’m sure there’s a better way to go through this, but it suited my needs to learn GOTO (Labels) and Multiples!

Turns out this also answers Brent Ozar’s ( t | b ) FizzBuzz interview question !

DECLARE @i INT DECLARE @msg NVARCHAR(500) SET @i = 1 WHILE @i <= 100 BEGIN SET @msg = '' IF @i%3 = 0 GOTO Fizz IF @i%5 = 0 GOTO Buzz ELSE print @i GOTO Increment Fizz: SET @msg = 'Fizz' if @i%5 = 0 GOTO Buzz PRINT @msg GOTO Increment Buzz: IF @msg = '' SET @msg = 'Buzz' else SET @msg = @msg + 'Buzz' PRINT @msg GOTO Increment Increment: SET @i = @i+1 END

Hope this was helpful to someone out there!


Viewing all articles
Browse latest Browse all 3160