sa_rowgenerator system procedure

Function

Returns a result set with rows between a specified start and end value.

Syntax

sa_rowgenerator (  [ rstart  [, rend  [, rstep ] ] ])

Parameters

Permissions

DBA authority required. Users without DBA authority must be granted EXECUTE permission to run the stored procedure.

Result sets

Column name

Data type

Description

row_num

integer

Sequence number.

Remarks

The sa_rowgenerator procedure can be used in the FROM clause of a query to generate a sequence of numbers. This procedure is an alternative to using the RowGenerator system table. You can use sa_rowgenerator for such tasks as:

You can emulate the behavior of the RowGenerator table with the following statement:

SELECT row_num FROM sa_rowgenerator(1255)

Side effects

None

Example

The following query returns a result set containing one row for each day of the current month:

SELECT dateadd(day,row_num-1,
ymd(datepart(year,CURRENT DATE),
datepart(month,CURRENT DATE), 1)) AS
day_of_month FROM sa_RowGenerator(1,31,1) WHERE
datepart(month,day_of_month) =
datepart(month,CURRENT DATE) ORDER BY row_num

The following query shows how many employees live in zip code ranges (0-9999), (10000-19999), ..., (90000-99999). Some of these ranges have no employees, which causes the warning Null value eliminated in aggregate function (-109). The sa_rowgenerator procedure can be used to generate these ranges, even though no employees have a PostalCode in the range.

SELECT row_num AS r1, row_num+9999 AS r2,
count(PostalCode) AS zips_in_range 
FROM sa_rowgenerator(0,99999,10000) D LEFT JOIN employees ON PostalCode 
BETWEEN r1 AND r2 GROUP BY r1, r2 ORDER BY 1

The following example generates 10 rows of data and inserts them into the emp table:

INSERT INTO emp1(dept_id, salary, name) SELECT row_num,
CAST( rand() * 1000 AS INTEGER), 'Mary' FROM
sa_rowgenerator(1, 10)