的Oracle SQL用表()函数问题
问题描述:
我做的,从甲骨文到pgsql的迁移,我已经对甲骨文的SQL像下面向pgsql:的Oracle SQL用表()函数问题
select code,product_no,qty qty from t_ma_tb_inventory
where code is not null and status=2
and update_type in (select * from table(splitstr(:types,',')))
and tb_shop_id=:shopId
order by update_time
和splitstr功能象下面这样:
?CREATE OR REPLACE FUNCTION splitstr (p_string text, p_delimiter text) RETURNS SETOF STR_SPLIT AS $body$
DECLARE
v_length bigint := LENGTH(p_string);
v_start bigint := 1;
v_index bigint;
BEGIN
WHILE(v_start <= v_length)
LOOP
v_index := INSTR(p_string, p_delimiter, v_start);
IF v_index = 0
THEN
RETURN NEXT SUBSTR(p_string, v_start);
v_start := v_length + 1;
ELSE
RETURN NEXT SUBSTR(p_string, v_start, v_index - v_start);
v_start := v_index + 1;
END IF;
END LOOP;
RETURN;
END
$body$
LANGUAGE PLPGSQL
SECURITY DEFINER
;
-- REVOKE ALL ON FUNCTION splitstr (p_string text, p_delimiter text) FROM PUBLIC;
有人可以帮我写在pgSQL的等价代码非常感谢您
答
你不需要编写自己的功能 - Postgres的已经有一个内置功能:string_to_array
select code, product_no, qty qty
from t_ma_tb_inventory
where code is not null and status=2
and update_type = any (string_to_array(:types,','))
and tb_shop_id = :shopId
order by update_time;
如果你传递一个文本,但你需要比较的是一个整数,你需要转换的结果数组:
select code, product_no, qty qty
from t_ma_tb_inventory
where code is not null and status=2
and update_type = any (string_to_array(:types,',')::int[])
and tb_shop_id = :shopId
order by update_time;
不过:这是一个很丑陋的解决方法这只在Oracle中是必需的,因为它不支持SQL中的实际数组(仅在PL/SQL中)。
在Postgres的这将是多更好直接传递整数数组(例如使:types
一个int[]
)。然后,没有分析或铸造是必要的:
select code, product_no, qty qty
from t_ma_tb_inventory
where code is not null and status=2
and update_type = any (:types)
and tb_shop_id = :shopId
order by update_time;
感谢,怎么样:?类型的参数是INT我该怎么办对不起,我是新向pgsql –
您还没有表现出你是如何运行的声明。但是我假定你使用的参数':types'有一个'text'值(因为你的函数也期望有'text')。你如何以及在哪里取代这些参数? Oracle中':types'的数据类型是什么? –
oracle中update_type的数据类型是int –