Nou*_*tti 4 postgresql stored-procedures function
我正在尝试根据 case 语句设置变量的值,并将数据作为 Postgres 函数中的表返回。
在 case 语句中,我尝试对选项字段进行按位运算
CREATE OR REPLACE FUNCTION get_ticket_types()
RETURNS TABLE (
ticket_type text
,sale_type text) AS
$func$
declare sale_type text;
BEGIN
RETURN QUERY
select name::text as ticket_type,
(case when (options & 1) > 0 then sale_type = concat(sale_type, 'POS ')
when (options & 2) > 0 then sale_type = concat(sale_type, 'Internet Jetstar ')
when (options & 4) > 0 then sale_type = concat(sale_type, 'Internet ')
when (options & 64) > 0 then sale_type = concat(sale_type, 'Internet FAPAS ')
when (options & 128) > 0 then sale_type = concat(sale_type, 'Internet Amadeus ')
when (options & 8) > 0 then sale_type = concat(sale_type, 'Agent ')
when (options & 16) > 0 then sale_type = concat(sale_type, 'Kiosk-Credit ')
when (options & 32) > 0 then sale_type = concat(sale_type, 'Kiosk-Cash ')
when (options & 32768) > 0 then sale_type = concat(sale_type, 'Preprinted ')
end )as sale_type
from skybus_tickettype
order by name asc;
END
$func$ LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)
这是行不通的。谁能告诉我这有什么问题吗?
它显示以下错误
错误:查询结构与函数结果类型不匹配详细信息:返回类型布尔值与第 2 列中的预期类型文本不匹配。上下文:PL/pgSQL 函数 get_ticket_types() 第 7 行位于 RETURN QUERY **********错误 **********
创建一个文本函数以从选项中获取串联类型名称:
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 $$;
Run Code Online (Sandbox Code Playgroud)
并以这种方式使用它:
select
name::text as ticket_type,
get_sale_type(options) as sale_type
from skybus_tickettype
order by name asc;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13900 次 |
| 最近记录: |