Example: Row data type use in an SQL function
Row data types can be used in SQL functions to construct, store, or modify record data.
Variables based on row data types can be used as a simple way to hold a row value that has the same format as a table. When used in this way, it is helpful to initialize the row variable upon its first use.
CREATE TABLE t1 (deptNo VARCHAR(3),
reportNo VARCHAR(3),
deptName VARCHAR(29),
mgrNo VARCHAR (8),
location VARCHAR(128))@
INSERT INTO t1 VALUES ('123', 'MM1', 'Sales-1', '0112345', 'Miami')@
INSERT INTO t1 VALUES ('456', 'MM2', 'Sales-2', '0221345', 'Chicago')@
INSERT INTO t1 VALUES ('789', 'MM3', 'Marketing-1', '0331299', 'Toronto')@
CREATE TYPE deptRow AS ROW (r_deptNo VARCHAR(3),
r_reportNo VARCHAR(3),
r_depTName VARCHAR(29),
r_mgrNo VARCHAR (8),
r_location VARCHAR(128))@
CREATE FUNCTION getLocation(theDeptNo VARCHAR(3),
reportNo VARCHAR(3),
theName VARCHAR(29))
RETURNS VARCHAR(128)
BEGIN
-- Declare a row variable
DECLARE dept deptRow;
-- Assign values to the fields of the row variable
SET dept.r_deptno = theDeptNo;
SET dept.r_reportNo = reportNo;
SET dept.r_deptname = theName;
SET dept.r_mgrno = '';
SET dept.r_location = '';
RETURN
(SELECT location FROM t1 WHERE deptNo = dept.r_deptno);
END@
VALUES (getLocation ('789', 'MM3','Marketing-1'))@
When executed this CLP script creates a table, inserts rows into the table, creates a row data type, and a UDF.
The function getLocation is an SQL UDF that declares a row variable and assigns values to it fields using the input parameter values. It references one of the fields in the row variable within the SELECT statement that defines the scalar value returned by the function.
When the VALUES statement is executed at the end of the script, the UDF is invoked and the scalar return value is returned.
CREATE TABLE t1 (deptNo VARCHAR(3), reportNo VARCHAR(3),
deptName VARCHAR(29), mgrNo VARCHAR (8), location VARCHAR(128))
DB20000I The SQL command completed successfully.
INSERT INTO t1 VALUES ('123', 'MM1', 'Sales-1', '0112345', 'Miami')
DB20000I The SQL command completed successfully.
INSERT INTO t1 VALUES ('456', 'MM2', 'Sales-2', '0221345', 'Chicago')
DB20000I The SQL command completed successfully.
INSERT INTO t1 VALUES ('789', 'MM3', 'Marketing-1', '0331299', 'Toronto')
DB20000I The SQL command completed successfully.
CREATE TYPE deptRow AS ROW (r_deptNo VARCHAR(3), r_reportNo VARCHAR(3), r_depTNa
me VARCHAR(29), r_mgrNo VARCHAR (8), r_location VARCHAR(128))
DB20000I The SQL command completed successfully.
CREATE FUNCTION getLocation(theDeptNo VARCHAR(3),
reportNo VARCHAR(3),
theName VARCHAR(29))
RETURNS VARCHAR(128)
BEGIN
DECLARE dept deptRow;
SET dept.r_deptno = theDeptNo;
SET dept.r_reportNo = reportNo;
SET dept.r_deptname = theName;
SET dept.r_mgrno = '';
SET dept.r_location = '';
RETURN
(SELECT location FROM t1 WHERE deptNo = dept.r_deptno);
END
DB20000I The SQL command completed successfully.
VALUES (getLocation ('789', 'MM3','Marketing-1'))
1
--------------------------------------------------------------------------------
------------------------------------------------
Toronto
1 record(s) selected.