Postgresql-如何在函数中设置变量值

Postgresql-如何在函数中设置变量值,postgresql,function,stored-procedures,Postgresql,Function,Stored Procedures,我试图根据case语句设置变量的值,并在Postgres函数中以表的形式返回数据 在case语句中,我试图对选项进行位运算 场 这是行不通的。谁能告诉我这有什么不对吗 它显示以下错误 错误:查询的结构与函数结果类型详细信息不匹配: 返回的类型布尔值与第2列中的预期类型文本不匹配。 上下文:PL/pgSQL函数get_ticket_types()返回查询时的第7行 **********错误********** 创建一个文本函数以从选项中获取连接的类型名称: create or replace fu

我试图根据case语句设置变量的值,并在Postgres函数中以表的形式返回数据

在case语句中,我试图对选项进行位运算 场

这是行不通的。谁能告诉我这有什么不对吗

它显示以下错误

错误:查询的结构与函数结果类型详细信息不匹配: 返回的类型布尔值与第2列中的预期类型文本不匹配。 上下文:PL/pgSQL函数get_ticket_types()返回查询时的第7行 **********错误**********


创建一个文本函数以从选项中获取连接的类型名称:

create or replace function get_sale_type(options int)
returns text language plpgsql as $$
declare
    sale_type text = '';
begin
    if options & 1 > 0 then sale_type:= concat(sale_type, 'POS '); end if;
    if options & 2 > 0 then sale_type:= concat(sale_type, 'Internet Jetstar '); end if; 
    if options & 4 > 0 then sale_type:= concat(sale_type, 'Internet '); end if;
    if options & 8 > 0 then sale_type:= concat(sale_type, 'Agent '); end if;
    if options & 16 > 0 then sale_type:= concat(sale_type, 'Kiosk-Credit '); end if;
    if options & 32 > 0 then sale_type:= concat(sale_type, 'Kiosk-Cash '); end if;
    if options & 64 > 0 then sale_type:= concat(sale_type, 'Internet FAPAS '); end if;
    if options & 128 > 0 then sale_type:= concat(sale_type, 'Internet Amadeus '); end if;
    if options & 32768 > 0 then sale_type:= concat(sale_type, 'Preprinted '); end if;
    return sale_type;
end $$;
并以这种方式使用它:

select 
    name::text as ticket_type, 
    get_sale_type(options) as sale_type
from skybus_tickettype
order by name asc;

创建一个文本函数以从选项中获取连接的类型名称:

create or replace function get_sale_type(options int)
returns text language plpgsql as $$
declare
    sale_type text = '';
begin
    if options & 1 > 0 then sale_type:= concat(sale_type, 'POS '); end if;
    if options & 2 > 0 then sale_type:= concat(sale_type, 'Internet Jetstar '); end if; 
    if options & 4 > 0 then sale_type:= concat(sale_type, 'Internet '); end if;
    if options & 8 > 0 then sale_type:= concat(sale_type, 'Agent '); end if;
    if options & 16 > 0 then sale_type:= concat(sale_type, 'Kiosk-Credit '); end if;
    if options & 32 > 0 then sale_type:= concat(sale_type, 'Kiosk-Cash '); end if;
    if options & 64 > 0 then sale_type:= concat(sale_type, 'Internet FAPAS '); end if;
    if options & 128 > 0 then sale_type:= concat(sale_type, 'Internet Amadeus '); end if;
    if options & 32768 > 0 then sale_type:= concat(sale_type, 'Preprinted '); end if;
    return sale_type;
end $$;
并以这种方式使用它:

select 
    name::text as ticket_type, 
    get_sale_type(options) as sale_type
from skybus_tickettype
order by name asc;