SQL Server ONES Function
Updated 2023-10-19 15:56:11.950000
Description
Use the scalar function ONES to generate an m-by-n matrix of ones.
Syntax
SELECT [westclintech].[wctMath].[wct].[ONES](
<@m, int,>
,<@n, int,>)
Arguments
@m
The number of rows in the ones matrix.
@n
The number of columns in the ones matrix.
Return Type
nvarchar(max)
Remarks
@m must be greater than or equal to 1.
@n must be greater than or equal to 1.
Examples
The following statement will produce the 5-by-5 ones matrix.
SELECT wct.ONES(5, 5) as ONES;
This produces the following result.
| ONES |
|---|
| 1,1,1,1,1;1,1,1,1,1;1,1,1,1,1;1,1,1,1,1;1,1,1,1,1 |
We can use the table-valued function MATRIX to produce the output in third-normal form.
SELECT *
FROM wct.MATRIX(wct.ONES(5, 5));
This produces the following result.
| RowNum | ColNum | ItemValue |
|---|---|---|
| 0 | 0 | 1 |
| 0 | 1 | 1 |
| 0 | 2 | 1 |
| 0 | 3 | 1 |
| 0 | 4 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
| 1 | 2 | 1 |
| 1 | 3 | 1 |
| 1 | 4 | 1 |
| 2 | 0 | 1 |
| 2 | 1 | 1 |
| 2 | 2 | 1 |
| 2 | 3 | 1 |
| 2 | 4 | 1 |
| 3 | 0 | 1 |
| 3 | 1 | 1 |
| 3 | 2 | 1 |
| 3 | 3 | 1 |
| 3 | 4 | 1 |
| 4 | 0 | 1 |
| 4 | 1 | 1 |
| 4 | 2 | 1 |
| 4 | 3 | 1 |
| 4 | 4 | 1 |