Skip to content

Commit

Permalink
Merge pull request #69 from Azure-Samples/sqlextension
Browse files Browse the repository at this point in the history
Adding the RANDOM function to the toolkit
  • Loading branch information
Matt Usher authored Dec 7, 2020
2 parents 9dec91b + 2c4af11 commit c45d170
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions SQL/Extension/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions SQL/Extension/deploy.bat
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions SQL/Extension/functions/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
23 changes: 23 additions & 0 deletions SQL/Extension/functions/microsoft.random.sql
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c45d170

Please sign in to comment.