USE [HRISDEV]
GO
/****** Object:  StoredProcedure [dbo].[proc_generate_random_string]    Script
Date: 15/05/2020 20.00.31 ******/ 
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[proc_generate_random_string] 
	@pi_min TINYINT = 5,
	@pi_max TINYINT = 20,
	@po_text VARCHAR(50) OUTPUT
AS
BEGIN

	SET NOCOUNT ON;-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.

	DECLARE
		@text nvarchar(255) = '', 
		@length int,
		@i int = 0;

	SET @length = RAND() * (@pi_max - @pi_min) + @pi_min 
	IF @length>@pi_max SET @length = @pi_max
	WHILE (@i < @length) 
		BEGIN
			WITH letters as
				(
					SELECT 'BCDFGHJKLMNPQRSTVWXYZ' c, 'AEIOU' v 
				),
				CTE as
					(
						SELECT
							CASE
								WHEN CRYPT_GEN_RANDOM(1) % 2 = 1 THEN SUBSTRING(v, CAST(rand()*5 as int)+1, 1)
								ELSE SUBSTRING(c, CAST(rand()*21 as int)+1, 1)
							END AS x
						FROM
							letters
					)
			SELECT @text = @text + x FROM CTE
			SET @i = @i + 1
		END
	SET @po_text = @text
END
GO