Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Temp table is not a known variable

I've got a temp table which I want to use twice to insert into it:

CREATE OR REPLACE FUNCTION test1(user_id BIGINT) RETURNS BIGINT AS
$BODY$
BEGIN
  DROP TABLE IF EXISTS temp_table1;
  create temp table temp_table1
  on commit drop
  as select * from get_some_data($1); -- try to get the data for the 1st time

  if not exists (select * temp_table1) then
    select try_to_update_data($1); -- updates data, returns void
  end if;

  -- try to get the data again
  select * into temp_table1 from get_some_data($1); -- error!
  if not exists (select * from temp_table1) then
    return 0;
  end if;

  --........ use temp_table1

It trows the error:

ERROR:  "temp_table1" is not a known variable

How do I solve this?

like image 771
Incerteza Avatar asked Sep 06 '25 03:09

Incerteza


1 Answers

write INSERT INTO temp_table1 SELECT get_some_data($1);

in place of what you had

select * into temp_table1 from get_some_data($1); -- error!

May be this will work for you.

like image 188
Ilesh Patel Avatar answered Sep 07 '25 20:09

Ilesh Patel