Using SELECT instead of conditionals with variables

You may have found yourself coding something like this in a Stored Procedure.

 
IF @pCode = 'AA' or @pCode = 'AB' or @pCode = 'BA' or @pCode = 'XY'
	SET @actionType = 'single'
ELSE
	SET @actionType = 'combined'
 

You can recode this to SQL to make it more readable.

The second option will be slower - about four times slower - but on my stone-age development system, it still performed 20000 iterations in under 200ms. That's 100000 comparisons per second.

 
SELECT
	@actionType = 'combined'
 
SELECT
	@actionType = 'single'
WHERE
	@pCode in
		(
			'AA',
			'AB',
			'BA',
			'XY'
		)
 


Leave a Reply