Returning table column names that are reserved key words?

66 Views Asked by At

I'm creating this function that returns a table. I have this issue in the 9th line in the code below, I believe the 'position' and the following 'attribute' column is the main problem. I want to return the column name exactly like in the code below. Anyone can help me? Thanks in advance!

  CREATE OR REPLACE FUNCTION acc_qry_chart_of_account ( 
  type CHARACTER (3)='GL') RETURNS TABLE ( seqno SERIAL,
        acc_id INTEGER,
        parent_code CHARACTER (20),
        acc_code CHARACTER (20),
        description CHARACTER VARYING (120),
        description2 CHARACTER VARYING (120),
        acclevel INTEGER,
        position CHARACTER VARYING (120),
        attribute INTEGER,
        acctype CHARACTER (3),
        exttype CHARACTER(3)
  )
AS $$
DECLARE
....
BEGIN
....
END;
$$ LANGUAGE 'plpgsql'
1

There are 1 best solutions below

0
On BEST ANSWER

You can solve this way. 2 changes.

  • Double quote column position
  • declare seqno as integer. Serial is not a datatype (it is a shortcut for using sequence)

New function:

  CREATE OR REPLACE FUNCTION acc_qry_chart_of_account ( 
  type CHARACTER (3)='GL') RETURNS TABLE ( seqno integer,
        acc_id INTEGER,
        parent_code CHARACTER (20),
        acc_code CHARACTER (20),
        description CHARACTER VARYING (120),
        description2 CHARACTER VARYING (120),
        acclevel INTEGER,
        "position" CHARACTER VARYING (120),
        attribute INTEGER,
        acctype CHARACTER (3),
        exttype CHARACTER(3)
  )
AS $$
DECLARE
....
BEGIN
....
END;
$$ LANGUAGE 'plpgsql'