0

I'm trying to create temp table and insert some data from LOOP. I need to count all strings on each table, but i have error like this:

ERROR: ОШИБКА:  переменная цикла по кортежам должна быть переменной типа запись или списком скалярных переменных
LINE 16: for r in (select "table_name", "name_campaign" from "Table_7...
             ^


SQL state: 42601
Character: 206

Table names all in one existing table.

 do
$body$

declare

   v_count integer;
    
begin

set enable_seqscan = off;

drop table if exists tmp_statyks;

create temp table tmp_statyks(table_name varchar(60), name varchar(60), x_count int);

for r in (select "table_name", "name_campaign" from "Table_7517829240") 
    loop
        execute immediate 'select count(*) from ' || r."table_name" 
            into v_count;
        INSERT INTO tmp_statyks("table_name", "name" , "x_count")
        VALUES (r."table_name",r."name_campaign",v_count);
    end loop;

end
$body$
language 'plpgsql';
select * from tmp_statyks
5
  • 1
    Could you please translate "переменная цикла по кортежам должна быть переменной типа запись или списком скалярных переменных" to English? Commented Apr 14, 2022 at 7:04
  • It seems you want to get the row count for a list of tables. See here for several solutions to that (including plain SQL) Commented Apr 14, 2022 at 7:08
  • @a_horse_with_no_name An error translation is: ERROR: loop variable of loop over rows must be a record variable or list of scalar variables Commented Apr 14, 2022 at 7:28
  • @Alex big thanks to you! I have troubles with russian error translates (:. And now I have error like: ERROR: ОШИБКА: тип "immediate" не существует LINE 1: SELECT immediate 'select count() from ' || r."table_name" ^ QUERY: SELECT immediate 'select count() from ' || r."table_name" CONTEXT: функция PL/pgSQL inline_code_block, строка 18, оператор EXECUTE Commented Apr 14, 2022 at 7:39
  • Because it is an embedded command for c and not psql postgresql.org/docs/14/ecpg-sql-commands.html You don't need immediate command in your case Commented Apr 14, 2022 at 7:52

1 Answer 1

1

Just declare r as record:

declare
   v_count integer;
   r record;
begin
Sign up to request clarification or add additional context in comments.

Comments

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.