0

I am creating function in postgresql to update table. when I ran single function it works fine but I use same query inside function it wont works.

update house1 SET calendar=
 'H'||' '||house_num::text||' '||left(to_char(sdate, 'Mon'),2) ||' to '  || to_char(sdate, 'DD') ||to_char(rdate, 'Mon-DD')
 where id=1

Inside function I wrote following query

begin
execute
'UPDATE house1 SET calendar = '
||quote_nullable( 'H')||' '||house_num||' '||left(to_char(sdate, 'Mon'),2) ||' to '  || to_char(sdate, 'DD') ||to_char(rdate, 'Mon-DD')
|| ' WHERE' ||  'stage'
'>=' || quote_nullable(0) ;
END

It generates following error

ERROR: column "house_num" does not exist.

Please help me

2
  • I don't think you need a dynamic statement in your function : EXECUTE (...). Just try to insert the first UPDATE statement in your function body and share the result. Commented Jan 8, 2022 at 7:45
  • you should be using format() to build dynamic SQL strings Commented Jan 8, 2022 at 20:19

1 Answer 1

1

Why do you use the execute statement in function?

You can use update statement directly in function (When using the execute statement if you need to build dynamic query)

create or replace function function_name()
   returns void 
   language plpgsql
  as
$$
begin
 update house1 SET calendar=
 'H'||' '||house_num::text||' '||left(to_char(sdate, 'Mon'),2) ||' to '  || to_char(sdate, 'DD') ||to_char(rdate, 'Mon-DD')
 where stage >= 0;
end;
$$;

select function_name();
Sign up to request clarification or add additional context in comments.

1 Comment

execute 'UPDATE ' || table_name_||' ' || 'SET ' || quote_ident(calCol) || '=' || quote_nullable(calConcat) || ' ' 'H' || house_num::text || ' S' || stage::text || ' ' || left(to_char(sdate, 'Mon'),2) ||' to ' || to_char(sdate, 'DD') || to_char(rdate, 'Mon-DD') || 'WHERE ' || quote_ident(calcol) || '>=' || quote_nullable(refCol); --'>=' || 0 ; but I want to create this function with dynamic parameter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.