A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/postgresql-row-type-variables/ below:

PostgreSQL - Row Type Variables

PostgreSQL - Row Type Variables

Last Updated : 23 Jul, 2025

In PostgreSQL, row type variables are handy when you need to store a whole row of data returned by a query. They are particularly useful when dealing with SELECT INTO statements where the result contains multiple columns from a table or view. This feature simplifies handling data from complex queries and makes your PL/pgSQL code more readable and maintainable.

In this article, we'll learn to declare and use row type variables in PostgreSQL, access specific fields within them using dot notation, and provide practical examples to help you understand their application.

What are Row Type Variables in PostgreSQL?

Row type variables in PostgreSQL allow you to store an entire row from a table or view in a single variable. The row is stored as a composite data type, which includes multiple columns that you can access individually.

These variables are declared using the '%ROWTYPE' attribute, which automatically matches the structure of the row to the table or view you reference. This feature is beneficial when working with complex queries that return multiple columns, allowing you to handle the data more flexibly.

How to Declare Row Type Variables

We can declare a row type variable that has the same data type as the corresponding datatype in the row of the table or the view using the following syntax.

Syntax: row_variable table_name/view_name %ROWTYPE;

We can use the dot notation (.) to access any field from the row variable.

Syntax: row_variable.field_name
PostgreSQL Row Type Variables Examples

First, we create a sample table using the below commands to perform examples:

PostgreSQL
CREATE TABLE employees (
 employee_id serial PRIMARY KEY,
 full_name VARCHAR NOT NULL,
 manager_id INT
);
INSERT INTO employees (
 employee_id,
 full_name,
 manager_id
)
VALUES
 (1, 'M.S Dhoni', NULL),
 (2, 'Sachin Tendulkar', 1),
 (3, 'R. Sharma', 1),
 (4, 'S. Raina', 1),
 (5, 'B. Kumar', 1),
 (6, 'Y. Singh', 2),
 (7, 'Virender Sehwag ', 2),
 (8, 'Ajinkya Rahane', 2),
 (9, 'Shikhar Dhawan', 2),
 (10, 'Mohammed Shami', 3),
 (11, 'Shreyas Iyer', 3),
 (12, 'Mayank Agarwal', 3),
 (13, 'K. L. Rahul', 3),
 (14, 'Hardik Pandya', 4),
 (15, 'Dinesh Karthik', 4),
 (16, 'Jasprit Bumrah', 7),
 (17, 'Kuldeep Yadav', 7),
 (18, 'Yuzvendra Chahal', 8),
 (19, 'Rishabh Pant', 8),
 (20, 'Sanju Samson', 8);

The table is:

Example 1: Selecting a Single Row with All Columns

The following will help create a row type variable sel_employee from the table employees

DO $$
DECLARE
sel_employee employees%ROWTYPE;
BEGIN
-- Select employee with ID 6
SELECT * INTO sel_employee
FROM employees
WHERE employee_id = 6;

RAISE NOTICE 'The employee name is % and the manager ID is %',
sel_employee.full_name,
sel_employee.manager_id;
END $$;

Output:

Explanation:

Example 2: Selecting a Single Row with Specific Columns

The following will help create a row type variable 'sel_employee' from the table employees with 2 columns 'employee_id' and 'full_name'.

DO $$
DECLARE
sel_employee employees%ROWTYPE;
BEGIN
-- Select employee with ID 12
SELECT employee_id, full_name
INTO sel_employee
FROM employees
WHERE employee_id = 12;

RAISE NOTICE 'The employee name is % and the length of the name is %',
sel_employee.full_name,
LENGTH(sel_employee.full_name);
END $$;

Output:

Explanation:

Important Points About PostgreSQL Row Type Variables


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4