Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database 在postgres循环中使用变量_Database_Postgresql - Fatal编程技术网

Database 在postgres循环中使用变量

Database 在postgres循环中使用变量,database,postgresql,Database,Postgresql,在循环中使用变量arrow时遇到问题。Postgres忽略声明的变量并将其作为文本读取。我怎样才能解决这个问题 我有以下错误: 错误:关系“arrow”不存在,其中:SQL语句“GRANT SELECT ON arrow TO test”PL/pgSQL函数内联\u code\u块在SQL语句的第8行 do $$ declare arrow record; BEGIN FOR arrow IN SELECT table_name FROM information_schema.t

在循环中使用变量
arrow
时遇到问题。Postgres忽略声明的变量并将其作为文本读取。我怎样才能解决这个问题

我有以下错误:
错误:关系“arrow”不存在,其中:SQL语句“GRANT SELECT ON arrow TO test”PL/pgSQL函数内联\u code\u块在SQL语句的第8行

do $$
declare
    arrow record;
BEGIN
FOR arrow IN
    SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'
LOOP
   GRANT SELECT ON arrow TO test;
END LOOP;
END;
$$ LANGUAGE plpgsql;

为此,您需要动态SQL

do $$
declare
    arrow record;
BEGIN
FOR arrow IN
    SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'
LOOP
   execute format('GRANT SELECT ON %I TO test', arrow.table_name);
END LOOP;
END;
$$ LANGUAGE plpgsql;

但是,在没有任何循环或PL/pgSQL的情况下,您可以更轻松地做到这一点:

grant select on all tables in schema public to test;