简体   繁体   中英

How to use variable inside function in plpgsql

i am storing a value coming from trigger into a variable inside a function.I am getting an error when i use the variable inside the function.Below is my function and trigger

CREATE OR REPLACE FUNCTION 
edm.automated_builder_update_trigger_manual()
RETURNS trigger AS
$BODY$
DECLARE
    e record;
    weekly_permit_table text := TG_ARGV[0];
BEGIN


Update  edm.weekly_permit_table as a
set applicant = applicant||' '||'-'||' '||new.builder where 
old.permit_number = split_part(a.permit_details, ' ',1);


RETURN NULL;
END;

And my trigger

CREATE TRIGGER builder_update_trigger_manual
 AFTER UPDATE
 ON edm.permit_table_manual_05_2017
 FOR EACH ROW
 WHEN (((old.builder)::text IS DISTINCT FROM (new.builder)::text))
 EXECUTE PROCEDURE 
edm.automated_builder_update_trigger_manual('weekly_permit_report_05_2017');

When i update builder value in 'edm.permit_table_manual_05_2017' table, am getting error 'ERROR: relation edm.weekly_permit_table doesnot exist'

I know this might be silly. But am not sure where am i wrong.

CREATE OR REPLACE FUNCTION edm.automated_builder_update_trigger_manual()
RETURNS trigger AS
$BODY$
DECLARE
  e record;
  weekly_permit_table text := TG_ARGV[0];
BEGIN
  EXECUTE 
    format(
      $q$
        UPDATE edm.%I AS a SET
          applicant = concat(applicant,' - '||$1)
        WHERE 
          $2 = split_part(a.permit_details, ' ',1)$q$,
      weekly_permit_table)
    USING NEW.builder, OLD.permit_number;
  RETURN NULL;
END $BODY$;

About EXECUTE

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