CREATE PROCEDURE dbo.ReservationCodeCreate @AReservationCode INT OUTPUT AS
SET NOCOUNT ON
/*
  Created by Kent Johansen, October 2019
  Return a random int value 

*/
DECLARE @Length INT
SELECT @Length = CONVERT(INT, KeyValue) FROM HotelSetup WITH (NOLOCK) WHERE SearchKey = 'ReferenceCodeLength'
SET @Length = ISNULL(@Length, 3)

DECLARE @Min INT
DECLARE @Max INT

SET @Min = '1' + REPLICATE('0', @Length-1)
SET @Max = '9' + REPLICATE('9', @Length-1)

SELECT @AReservationCode = FLOOR(RAND() * (@Max - @Min + 1)) + @Min