gpt4 book ai didi

sql - 数据库无法识别创建的函数? SQL 状态 : 42883

转载 作者:行者123 更新时间:2023-11-29 12:04:36 31 4
gpt4 key购买 nike

NULL 我的函数已成功创建,但是当我尝试使用它时出现错误消息:

ERROR: function coalesce2(character varying, character varying, character varying, character varying, character varying, character varying, character varying, character varying, character varying, character varying, character varying, character varying, character varying, character varying, character varying) does not exist SQL state: 42883 Hint: No function matches the given name and argument types. You may need to add explicit type casts.

我的函数的目的是获取第一个 NON NULL 值并将其与它右侧的值进行比较。如果右边的值不是 NULL,那么我将使用该值,如果是,那么我将使用 COALESCE 值。我的功能如下:

CREATE OR REPLACE FUNCTION coalesce2(variadic anyarray)
RETURNS anyelement AS
$BODY$
BEGIN
FOR i IN 1 .. array_upper($1, 1)
LOOP
IF $1[i] IS NOT NULL THEN
RETURN COALESCE($1[i+1], $1[i]);
END IF;
END LOOP;
RETURN NULL;
END
$BODY$
LANGUAGE plpgsql IMMUTABLE;

下面是正在使用的函数:

coalesce2 ( vw_header_to_node_13.subsetname, vw_header_to_node_12.subsetname, vw_header_to_node_11.subsetname, vw_header_to_node_10.subsetname, vw_header_to_node_9.subsetname, vw_header_to_node_8.subsetname, vw_header_to_node_7.subsetname, vw_header_to_node_6.subsetname, vw_header_to_node_5.subsetname, vw_header_to_node_4.subsetname, vw_header_to_node_3.subsetname, vw_header_to_node_2.subsetname, vw_header_to_node_1.subsetname, vw_header_to_node.subsetname, vw_header_to_node.setname) AS prctr2

我对函数没有太多经验,我不明白为什么它不能识别新创建的函数。非常感谢任何建议。

最佳答案

您需要将一个 ARRAY 传递给您的函数。

SELECT coalesce2(ARRAY['one', 'two', 'three']);

但是这个功能会很贵!我不建议像这样在内联 SQL 中使用 PL/pgSQL 函数。您最好使用 CASE 语句并创建一个不依赖于使用函数来格式化数据的新表。

然后回答关于如何改用 CASE 语句的第二个问题。

注意:您的 SQL 示例让我很担心,因为看起来您正在执行 10 个代价高昂的 LEFT OUTER JOIN 语句。它还以“vw_”为前缀,这让我觉得您有 10 个 VIEWS,而 VIEWS 也可以隐藏非常糟糕的 SQL。

我希望您没有使用 VIEWS 和大量的 LEFT OUTER JOIN 语句。分析最好用一个大的平面表来完成,其中包含您需要的每个属性,这些属性是面向列存储的,或者是一个经典的星型模式。转换一次数据,然后使用该输出进行分析。

关于答案。这是一个示例表,其中包含一些与您的相似的数据:

    drop table if exists foo;
create table foo
(id int not null,
col1 text,
col2 text,
col3 text,
col4 text,
col5 text,
col6 text,
col7 text,
col8 text,
col9 text,
col10 text)
distributed by (id);

insert into foo (id, col1, col2) values (1, 'x1', 'x1');
insert into foo (id, col2, col3) values (2, 'x2', 'x2');
insert into foo (id, col3, col4) values (3, 'x3', 'x3');
insert into foo (id, col4, col5) values (4, 'x4', 'x4');
insert into foo (id, col5, col6) values (5, 'x5', 'x5');
insert into foo (id, col6, col7) values (6, 'x6', 'x6');
insert into foo (id, col7, col8) values (7, 'x7', 'x7');
insert into foo (id, col8, col9) values (8, 'x8', 'x8');
insert into foo (id, col9, col10) values (9, 'x9', 'x9');
insert into foo (id, col10) values (10, 'x10');

这就是 CASE 语句的样子:

    select id, case when col1 is not null then coalesce(col2, col3, col4, col5, col6, col7, col8, col9, col10) 
when col2 is not null then coalesce(col3, col4, col5, col6, col7, col8, col9, col10)
when col3 is not null then coalesce(col4, col5, col6, col7, col8, col9, col10)
when col4 is not null then coalesce(col5, col6, col7, col8, col9, col10)
when col5 is not null then coalesce(col6, col7, col8, col9, col10)
when col6 is not null then coalesce(col7, col8, col9, col10)
when col7 is not null then coalesce(col8, col9, col10)
when col8 is not null then coalesce(col9, col10)
when col9 is not null then coalesce(col10)
else col10 end
from foo
order by id;

现在作为 SQL FUNCTION(不是 PL/pgSQL):

    create or replace function fn_coalesce2(text, text, text, text, text, text, text, text, text, text) returns text as
$$
select case when $1 is not null then coalesce($2, $3, $4, $5, $6, $7, $8, $9, $10)
when $2 is not null then coalesce($3, $4, $5, $6, $7, $8, $9, $10)
when $3 is not null then coalesce($4, $5, $6, $7, $8, $9, $10)
when $4 is not null then coalesce($5, $6, $7, $8, $9, $10)
when $5 is not null then coalesce($6, $7, $8, $9, $10)
when $6 is not null then coalesce($7, $8, $9, $10)
when $7 is not null then coalesce($8, $9, $10)
when $8 is not null then coalesce($9, $10)
when $9 is not null then coalesce($10)
else $10 end;
$$
language sql;

select id, fn_coalesce2(col1, col2, col3, col4, col5, col6, col7, col8, col9, col10)
from foo
order by id;

id | fn_coalesce2
----+--------------
1 | x1
2 | x2
3 | x3
4 | x4
5 | x5
6 | x6
7 | x7
8 | x8
9 | x9
10 | x10
(10 rows)

关于sql - 数据库无法识别创建的函数? SQL 状态 : 42883,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33423894/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com