简体   繁体   中英

postgresql: relation does not exist

I'm new using pl pgsql, with my pseudo code below i'm getting this error:

relation "Client.seniority_day" does not exist

How to resolve it please?

code

DROP FUNCTION IF EXISTS table_sen_seniority;
CREATE OR REPLACE FUNCTION Client.table_sen_seniority(granularity text, OUT max_date date)
RETURNS record
LANGUAGE plpgsql
AS $function$
declare
    table_sen text := 'Client.seniority'||granularity;

 BEGIN
     execute format('SELECT MIN(dat_sub) FROM %I', table_sen) INTO subscription_from;
     execute format('SELECT FROM %I WHERE date_ >= $1', table_sen) using subscription_from;
 END;
$function$
 ;

update

execute format('SELECT FROM %I WHERE date_ >= $1', table_sen) using subscription_from;

You need to pass the schema and the table:

table_sen text := 'seniority'||granularity;

execute format('SELECT MIN(dat_sub) FROM %I.%I', 'Client', table_sen) 
  INTO subscription_from;

Note that "Client" and client would be different schema names.

Your code results in

SELECT MIN(dat_sub) FROM "Client.seniority_day"

which treats the whole string as a table name, not a table called seniority_day in a schema called client .

You should do it like this:

DECLARE
   table_sen text := 'seniority' || granularity;
BEGIN
   EXECUTE format('SELECT MIN(dat_sub) FROM %I.%I', 'client', table_sen)
      INTO subscription_from;

That will result in

SELECT MIN(dat_sub) FROM client.seniority_day

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM