【问题标题】:Unable to access Information_schema via stored procedure无法通过存储过程访问 Information_schema
【发布时间】:2019-09-09 11:13:45
【问题描述】:

我正在编写一个涉及在 AWS Redshift 中使用信息架构的存储过程。

以下所有变量 -

  1. 使用同一用户
  2. 使用相同的 Redshift 数据库(端点)

存储过程:

create or replace procedure dev.gp_information_schema_test
    (tablename varchar(64))
as $$
declare 
table_name varchar(64);
schema_name varchar(64);
counts int;
begin
table_name := split_part(tablename,'.',1);
schema_name:= split_part(tablename,'.',2);
raise info 'table_name - %,Schema_name - %',table_name,schema_name;
counts := (select count(*) from information_schema.tables where table_schema = schema_name);
raise info 'count is -%',counts;
end;
$$
language plpgsql

call dev.gp_information_schema_test('dev.abc');

结果:

Warnings:
table_name - dev,Schema_name - abc
count is -0

0 rows affected
call executed successfully

Execution time: 0.55s

但如果我在外部运行相同的查询(即不通过存储过程),那么:

select count(*) 
from information_schema.tables 
where table_schema = 'dev'

结果:

我已经阅读了 AWS 文档 (Link) 中存储过程的限制,但没有提到对系统表的访问限制。

【问题讨论】:

  • 感谢@marc_S 正确编辑问题。下次会注意的。

标签: amazon-web-services amazon-redshift


【解决方案1】:

您不能使用:= 从查询中设置变量的值。相反,您需要使用SELECT INTO variable 表单。 https://docs.aws.amazon.com/redshift/latest/dg/c_PLpgSQL-structure.html

试试这个 SP:

CREATE OR REPLACE PROCEDURE gp_information_schema_test(tablename VARCHAR(64)) AS
$$
DECLARE
    table_name  VARCHAR(64);
    schema_name VARCHAR(64);
    counts      INT;
BEGIN
    schema_name := split_part(tablename, '.', 1);
    table_name := split_part(tablename, '.', 2);
    RAISE INFO 'table_name - % , Schema_name - %',table_name,schema_name;
    counts := (SELECT count(*) FROM information_schema.tables WHERE table_schema = schema_name);
    RAISE INFO 'Tables in schema: %',counts;
END;
$$ LANGUAGE plpgsql;

呼叫:

CALL gp_information_schema_test('dev.abc');

【讨论】:

  • 嘿@joe,这不是我在问题中写的一样吗?但由于某种原因,它返回了正确的结果
  • 在上面,如果我添加另一个 WHERE 过滤器然后它再次停止工作并给我以前的结果(不正确的结果),一种缓存的结果。即查询 - SELECT count(*) FROM information_schema.tables WHERE table_schema = schema_name and table_name like '%abc%' 给我的结果与没有 --> table_name like '%abc%'的结果相同>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-18
  • 1970-01-01
  • 2021-10-29
  • 2019-01-14
  • 1970-01-01
相关资源
最近更新 更多