Stored procedures 存储过程中给出错误结果的计数

Stored procedures 存储过程中给出错误结果的计数,stored-procedures,amazon-redshift,Stored Procedures,Amazon Redshift,我从存储过程中运行的查询中得到了不正确的计数。 当我们运行相同的查询时(在硬编码表名和模式名的值之后),它会给出正确的结果 初步分析表明,由于某种原因,在存储过程中运行的查询忽略了第二个过滤器(即,其中…和…,忽略了第二部分) 并将上述程序称为— call dev.gp_count ('dev.gp_test1'); 从存储过程获得的结果是—— 警告: 检查计数\u 1-925 如果我们在替换表名和模式的值后运行相同的查询,则- select count(*) from information

我从存储过程中运行的查询中得到了不正确的计数。 当我们运行相同的查询时(在硬编码表名和模式名的值之后),它会给出正确的结果

初步分析表明,由于某种原因,在存储过程中运行的查询忽略了第二个过滤器(即,其中…和…,忽略了第二部分)

并将上述程序称为—

call dev.gp_count ('dev.gp_test1');
从存储过程获得的结果是—— 警告: 检查计数\u 1-925

如果我们在替换表名和模式的值后运行相同的查询,则-

select count(*) from information_schema.tables where table_schema = 'dev' and table_name like '%gp_test1%';
结果-

现在进一步调查这个问题-

从存储过程获得的计数与从此查询获得的计数相同-

select count(*) from information_schema.tables where table_schema = 'dev';
结果-

我猜-

所以这暗示着,可能在存储过程中,第二个过滤条件被忽略了

除了帮助我做其他选择外,请帮助我找到这种异常现象背后的原因


提前谢谢。

我认为您的问题在于1)字符串连接和2)使用
表名作为变量:

check_count_1 := (select count(*) from information_schema.tables where table_schema = schema_name and table_name like '%' + table_name +'%');
PostgreSQL字符串连接使用
|
,因此应该如下所示:

check_count_1 := (select count(*) from information_schema.tables where table_schema = schema_name and table_name like '%' || table_name || '%');
CREATE OR REPLACE PROCEDURE gp_count (tablename VARCHAR(256))
AS
$$ DECLARE
schema_name VARCHAR(64);
table_name1 VARCHAR(128);
check_count_1 INT;
check_count_2 INT;

BEGIN 
schema_name:= SPLIT_PART(tablename,'.',1);
table_name1:= SPLIT_PART(tablename,'.',2);

check_count_1 := (select count(*) from information_schema.tables f where table_schema = schema_name and f.table_name like '%' || table_name1 || '%');

raise info 'check_count_1 - %',check_count_1;

end;
$$
language plpgsql;
尝试将其更改为如下所示:

check_count_1 := (select count(*) from information_schema.tables where table_schema = schema_name and table_name like '%' || table_name || '%');
CREATE OR REPLACE PROCEDURE gp_count (tablename VARCHAR(256))
AS
$$ DECLARE
schema_name VARCHAR(64);
table_name1 VARCHAR(128);
check_count_1 INT;
check_count_2 INT;

BEGIN 
schema_name:= SPLIT_PART(tablename,'.',1);
table_name1:= SPLIT_PART(tablename,'.',2);

check_count_1 := (select count(*) from information_schema.tables f where table_schema = schema_name and f.table_name like '%' || table_name1 || '%');

raise info 'check_count_1 - %',check_count_1;

end;
$$
language plpgsql;

披露:我为红班或博士后工作?虽然它们有一些共同的根,但它们是非常不同的数据库产品。我正在使用PLPGSQL作为存储过程的语言(在Postgresql中),并在Redshift.interest上执行它。谢谢分享!谢谢@richyn,它的效果很好。仅供参考-PostgreSQL确实支持+或| |的连接。这两种选择都有效。
CREATE OR REPLACE PROCEDURE gp_count (tablename VARCHAR(256))
AS
$$ DECLARE
schema_name VARCHAR(64);
table_name1 VARCHAR(128);
check_count_1 INT;
check_count_2 INT;

BEGIN 
schema_name:= SPLIT_PART(tablename,'.',1);
table_name1:= SPLIT_PART(tablename,'.',2);

check_count_1 := (select count(*) from information_schema.tables f where table_schema = schema_name and f.table_name like '%' || table_name1 || '%');

raise info 'check_count_1 - %',check_count_1;

end;
$$
language plpgsql;