Sql Table Function - Memorable Alias Generator
Wanted to create a User Defined Function in MS SQL server that would generate memorable aliases for users to use.
First point is that a UDF function does not allow you to create temp tables within it or use side effecting functions like NEWID() which was not fun, but you can do the following.
For unique ids create a view that wraps the NEWID() function as such
This will give you a reasonable random float number from 0 to 1 each time you select from it.
CREATE VIEW [dbo].[RandomView] AS SELECT RAND(convert(varbinary, newid())) as RandomValue
GO
Next for temp tables in a udf function you can make an inline table variable to get around that and since the tables are all small they will fit in memory so this is valid for getting around temp tables
DECLARE @Animal Table([Id] [int] IDENTITY(1,1) NOT NULL, [Name] [varchar](20) NOT NULL);
And then you can insert all your values like so
INSERT INTO @Animal SELECT ('aardvark')
Finally here is the memorable alias function that can generate easy to remember names like black-buffalo that are not leaking identifying information.
CREATE FUNCTION [dbo].[Alias]
(
)
RETURNS varchar(40)
AS
BEGIN
DECLARE @random varchar(40);
DECLARE @Animal Table([Id] [int] IDENTITY(1,1) NOT NULL, [Name] [varchar](20) NOT NULL);
INSERT INTO @Animal VALUES ('aardvark')
,('antelope')
,('bass')
,('bear')
,('boar')
,('bonobo')
,('buffalo')
,('calf')
,('carp')
,('catfish')
,('cavy')
,('cheetah')
,('chicken')
,('chub')
,('clam')
,('crab')
,('crayfish')
,('crow')
,('deer')
,('dogfish')
,('dolphin')
,('dove')
,('duck')
,('elephant')
,('flamingo')
,('flea')
,('frog')
,('fruitbat')
,('giraffe')
,('girl')
,('gnat')
,('goat')
,('gorilla')
,('gull')
,('haddock')
,('hamster')
,('hare')
,('hawk')
,('herring')
,('honeybee')
,('housefly')
,('kiwi')
,('ladybird')
,('lark')
,('leopard')
,('lion')
,('lobster')
,('lynx')
,('mink')
,('mole')
,('mongoose')
,('moth')
,('newt')
,('octopus')
,('opossum')
,('oryx')
,('ostrich')
,('parakeet')
,('penguin')
,('pheasant')
,('pike')
,('piranha')
,('pitviper')
,('platypus')
,('polecat')
,('pony')
,('porpoise')
,('puma')
,('pussycat')
,('raccoon')
,('reindeer')
,('rhea')
,('scorpion')
,('seahorse')
,('seal')
,('sealion')
,('seasnake')
,('seawasp')
,('skimmer')
,('skua')
,('slowworm')
,('slug')
,('sole')
,('sparrow')
,('squirrel')
,('starfish')
,('stingray')
,('swan')
,('termite')
,('toad')
,('tortoise')
,('tuatara')
,('tuna')
,('vampire')
,('vole')
,('vulture')
,('wallaby')
,('wasp')
,('wolf')
,('worm')
,('wren')
DECLARE @Color Table([Id] [int] IDENTITY(1,1) NOT NULL, [Name] [varchar](20) NOT NULL);
INSERT INTO @Color VALUES ('agate')
,('anthracite')
,('antique')
,('azure')
,('basalt')
,('beige')
,('black')
,('blue')
,('bottle')
,('bright')
,('brillant')
,('broom')
,('brown')
,('capri')
,('carmine')
,('cement')
,('chestnut')
,('chocolate')
,('chrome')
,('claret')
,('clay')
,('cobalt')
,('concrete')
,('copper')
,('coral')
,('daffodil')
,('dahlia')
,('deep')
,('distant')
,('dusty')
,('emerald')
,('fawn')
,('fern')
,('fir')
,('flame')
,('gentian')
,('golden')
,('granite')
,('graphite')
,('grass')
,('green')
,('grey')
,('heather')
,('honey')
,('iron')
,('jet')
,('khaki')
,('leaf')
,('lemon')
,('light')
,('luminous')
,('mahogany')
,('maize')
,('may')
,('melon')
,('mint')
,('moss')
,('mouse')
,('night')
,('nut')
,('ocean')
,('ochre')
,('olive')
,('opal')
,('orange')
,('orient')
,('oxide')
,('oyster')
,('pale')
,('papyrus')
,('pastel')
,('patina')
,('pearl')
,('pebble')
,('pigeon')
,('pine')
,('platinum')
,('pure')
,('purple')
,('quartz')
,('raspberry')
,('red')
,('reed')
,('reseda')
,('ruby')
,('saffron')
,('salmon')
,('sand')
,('saphire')
,('sepia')
,('signal')
,('silk')
,('silver')
,('sky')
,('slate')
,('squirrel')
,('steel')
,('stone')
,('strawberry')
,('sulfur')
,('sun')
,('tarpaulin')
,('telegrey')
,('terra')
,('tomato')
,('traffic')
,('turquoise')
,('ultramarine')
,('umbra')
,('violet')
,('water')
,('white')
,('wine')
,('yellow')
,('zinc')
DECLARE @animalSeed float = (SELECT RandomValue From RandomView);
DECLARE @colorSeed float = (SELECT RandomValue From RandomView);
DECLARE @maxAnimalId int = (SELECT MAX(ID) FROM @Animal);
DECLARE @minAnimalId int = (SELECT MIN(ID) FROM @Animal);
DECLARE @maxColorId int = (SELECT MAX(ID) FROM @Color);
DECLARE @minColorId int = (SELECT MIN(ID) FROM @Color);
DECLARE @animalId int = ROUND(((@maxAnimalId - @minAnimalId -1) * @animalSeed + @minAnimalId), 0);
DECLARE @colorId int = ROUND(((@maxColorId - @minColorId -1) * @colorSeed + @minColorId), 0);
DECLARE @animalName varchar(20) = (SELECT [Name] FROM @Animal WHERE ID = @animalId);
DECLARE @colorName varchar(20) = (SELECT [Name] FROM @Color WHERE ID = @colorId);
select @random = SUBSTRING(@colorName + '-' + @animalName, 1, 40)
RETURN @random;
END