2017-08-31 45 views
0

我做的,從甲骨文到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的等價代碼非常感謝您

回答

0

你不需要編寫自己的功能 - 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; 
+0

感謝,怎麼樣:?類型的參數是INT我該怎麼辦對不起,我是新向pgsql –

+0

您還沒有表現出你是如何運行的聲明。但是我假定你使用的參數':types'有一個'text'值(因爲你的函數也期望有'text')。你如何以及在哪裏取代這些參數? Oracle中':types'的數據類型是什麼? –

+0

oracle中update_type的數據類型是int –