gpt4 book ai didi

sql - PostgreSQL:可以创建具有 TYPE 的函数并在之后修改 TYPE

转载 作者:行者123 更新时间:2023-11-29 12:38:46 24 4
gpt4 key购买 nike

您好,我想创建一个返回用户定义类型的函数。创建函数后,我希望能够将类型更新为其他类型,以便函数使用新类型,例如。

CREATE TYPE my_type AS ("Column1" text, "Column2" text);

CREATE OR REPLACE FUNCTION my_fucntion()
RETURNS SETOF my_type
bla
bla
blah

ALTER TYPE my_type AS ("Name" text, "Address" text)

这可能吗?更改将始终只是列的名称,而不是实际的数据格式,它们始终是相同的。

编辑:好吧,问题是我有 2 个脚本,其中一个是通用的,必须按原样安装,不做任何更改。它必须安装在多个地方。第二个是一种配置脚本。

在通用脚本中,我创建了一个函数,它返回我想在配置脚本中定义的类型。

我想先安装配置脚本,但我不能,因为我需要根据通用脚本中的功能执行一些插入操作。

谢谢

最佳答案

我尝试使用 PostgreSQL 9.1,它似乎可以正常工作。示例:

CREATE TYPE my_type as (col1 text, col2 text);
CREATE TABLE my_table (a my_type);
INSERT INTO my_table values(('ab','cd'));
CREATE FUNCTION my_function() returns setof my_type as
'begin return query select (a).* from my_table; end;'
language plpgsql;

select * from my_function();

输出是:

 col1 | col2 
------+------
ab | cd

现在让我们重命名 my_type 中的列:

alter type my_type rename attribute col1 to address;
alter type my_type rename attribute col2 to name;

=> 没有错误

现在再次调用函数:

select * from my_function();

没有错误,输出为:

 address | name 
---------+------
ab | cd

看起来像预期的结果,不是吗?

关于sql - PostgreSQL:可以创建具有 TYPE 的函数并在之后修改 TYPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9634974/

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