pgsql code is not working.

From: "Singh, Gambhir" <gsingh2(at)kemper(dot)com>
To: "pgsql-admin(at)lists(dot)postgresql(dot)org" <pgsql-admin(at)lists(dot)postgresql(dot)org>
Subject: pgsql code is not working.
Date: 2023-05-12 07:34:21
Message-ID: PH7PR10MB6457C715E795825C224B77949F759@PH7PR10MB6457.namprd10.prod.outlook.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

Hi All,

I was trying to execute the below mentioned code but to not able to make it workable.

Table Name - additional_details
Sequence Name - additional_details_id_seq

DO $$
DECLARE PKEY VARCHAR;
DECLARE MAX_VAL INTEGER;
BEGIN

SELECT pg_attribute.attname INTO PKEY
FROM pg_index, pg_class, pg_attribute
WHERE pg_class.oid = 'additional_details'::regclass
AND indrelid = pg_class.oid
AND pg_attribute.attrelid = pg_class.oid
AND pg_attribute.attnum = any(pg_index.indkey)
AND indisprimary;

SELECT MAX(pkey) into MAX_VAL FROM additional_details;
SELECT setval(additional_details_id_seq,MAX_VAL + 1);

Raise notice 'Value pkey: %',pkey;
Raise notice 'Value max_val: %',max_val;
END;
$$ LANGUAGE plpgsql;

ERROR: invalid input syntax for type integer: "additional_details_id"
CONTEXT: PL/pgSQL function inline_code_block line 14 at SQL statement
SQL state: 22P02

But when I change the data type of 'MAX_VAL' variable from INTEGER to VRACHAR then got this result.

ERROR: operator does not exist: character varying + integer
LINE 1: SELECT setval('additional_details_id_seq',MAX_VAL + 1)
^
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
QUERY: SELECT setval('additional_details_id_seq',MAX_VAL + 1)
CONTEXT: PL/pgSQL function inline_code_block line 15 at SQL statement
SQL state: 42883

To know the values stored in variable I commented the "SELECT setval" line, after that I got this result.

NOTICE: Value pkey: additional_details_id
NOTICE: Value max_val: additional_details_id

Actually in max_val column, it should display the max value of column in numbers, instead it showing the same value which is stored in pkey variable.

Please help me to solve this issue.

Thanks & Regards
Gambhir Singh

____
Gambhir Singh
Contractors - Projects
Kemper Corporate Services
Chicago, IL 60601
E gsingh2(at)kemper(dot)com
kemper.com

CONFIDENTIALITY NOTICE: This communication may contain
confidential information intended only for the addressee(s). If you received
this communication in error, please notify the sender and delete it from your
system.

Responses

Browse pgsql-admin by date

  From Date Subject
Next Message fatih abztn 2023-05-12 08:57:20 ERROR: invalid message format and FATAL: invalid frontend message type XX logs
Previous Message kyle Hailey 2023-05-12 00:02:19 Re: ATTACH PARTITION "hangs"