diff --git a/SQL/Extension/Readme.md b/SQL/Extension/Readme.md index 71569ed..85dd7c6 100644 --- a/SQL/Extension/Readme.md +++ b/SQL/Extension/Readme.md @@ -31,6 +31,10 @@ The Azure Synapse SQL Extension will install a collection of vies to your Synaps ## Version History +### December 7, 2020 +What's new: +- [Function]: The `microsoft.random` script installs a function that emulates the `RANDOM(expression, expression)` Teradata function. + ### December 1, 2020 What's new: - [Function]: The `microsoft.to_char` script installs a function that emulates the `TO_CHAR(expression, format)` Teradata function. diff --git a/SQL/Extension/deploy.bat b/SQL/Extension/deploy.bat index 22a904e..ccc873f 100644 --- a/SQL/Extension/deploy.bat +++ b/SQL/Extension/deploy.bat @@ -57,6 +57,7 @@ sqlcmd -S %_server% -d %_database% -U %_username% -P %_password% -I -i .\functio sqlcmd -S %_server% -d %_database% -U %_username% -P %_password% -I -i .\functions\microsoft.ltrim.sql sqlcmd -S %_server% -d %_database% -U %_username% -P %_password% -I -i .\functions\microsoft.months_between.sql sqlcmd -S %_server% -d %_database% -U %_username% -P %_password% -I -i .\functions\microsoft.next_day.sql +sqlcmd -S %_server% -d %_database% -U %_username% -P %_password% -I -i .\functions\microsoft.random.sql sqlcmd -S %_server% -d %_database% -U %_username% -P %_password% -I -i .\functions\microsoft.rpad.sql sqlcmd -S %_server% -d %_database% -U %_username% -P %_password% -I -i .\functions\microsoft.rtrim.sql sqlcmd -S %_server% -d %_database% -U %_username% -P %_password% -I -i .\functions\microsoft.to_char.sql diff --git a/SQL/Extension/functions/Readme.md b/SQL/Extension/functions/Readme.md index 33d593d..6dff562 100644 --- a/SQL/Extension/functions/Readme.md +++ b/SQL/Extension/functions/Readme.md @@ -33,6 +33,9 @@ The `microsoft.months_between` script installs a function that emulates the `MON ## microsoft.next_day The `microsoft.next_day` script installs a function that emulates the `NEXT_DAY(date_expression, day_value)` Teradata function. +## microsoft.random +The `microsoft.random` script installs a function that emulates the `RANDOM(expression, expression)` Teradata function. + ## microsoft.rpad The `microsoft.rpad` script installs a function that emulates the `RPAD(expression, length [, fill])` Teradata function. diff --git a/SQL/Extension/functions/microsoft.random.sql b/SQL/Extension/functions/microsoft.random.sql new file mode 100644 index 0000000..69c1136 --- /dev/null +++ b/SQL/Extension/functions/microsoft.random.sql @@ -0,0 +1,23 @@ +IF EXISTS (SELECT * FROM sys.objects WHERE schema_id=SCHEMA_ID('microsoft') AND name = N'random') + DROP FUNCTION microsoft.random; +GO + +CREATE FUNCTION microsoft.random(@lower_bound INT, @upper_bound INT, @random float) +RETURNS INT +WITH SCHEMABINDING +AS +BEGIN + + -- Add error handling when Azure Synapse SQL supports THROW + --IF (@lower_bound > @upper_bound) + -- BEGIN + -- THROW 51000, 'The RANDOM function has invalid arguments.'; + -- END + + -- Declarations + DECLARE @range INT = (@upper_bound - @lower_bound + 1); + + RETURN FLOOR( ( @random * @range ) + @lower_bound); + +END +GO \ No newline at end of file