Problem When creating a function in PostgreSQL "ERROR: syntax error in or near" ROWTYPE ""

The following is a simple procedure in PL / SQL

PROCEDURE emp_get_rec (emp_rec IN OUT NOCOPY emp_content%ROWTYPE)
  IS
    v_cnt   NUMBER;
  BEGIN
    SELECT COUNT(*)
    INTO v_cnt
    FROM emp_content
    WHERE emp_id = emp_rec.emp_id;
    IF v_cnt = 1
    THEN
      SELECT * INTO emp_rec
      FROM emp_content
      WHERE emp_id = emp_rec.emp_id;
    END IF;
 END emp_get_rec;

What I'm trying to convert to PostgreSQL,

Create or replace function emp_get_rec (emp_rec IN OUT emp_content%ROWTYPE)
AS $BODY$
DECLARE
    v_cnt   NUMBER;
BEGIN

       SELECT COUNT(*)
    INTO v_cnt
    FROM emp_content
    WHERE emp_id = emp_rec.emp_id;
    IF v_cnt = 1
    THEN
      SELECT * INTO emp_rec
      FROM emp_content
      WHERE emp_id = emp_rec.emp_id;
    END IF;
 END;
$BODY$ LANGUAGE 'plpgsql';

I encountered the following error:

ERROR: syntax error at or near "ROWTYPE"

+4
source share
1 answer

The documentation says :

Whenever you create a table, a composite type is also automatically created with the same name as the table to represent the table row type.

Thus, you can use the table name as the type name, where syntactically this means type:

create or replace function emp_get_rec (emp_rec IN OUT emp_content)
+1
source

Source: https://habr.com/ru/post/1681602/


All Articles