2017-08-31 1 views
0

Ich mache die Migration von Oracle auf pgsql und ich habe das die Oracle SQL wie unten:Oracle SQL mit Tabelle() Funktion Problem pgsql

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 

und die splitstr Funktion wie unten:

?
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; 

kann mir jemand helfen, den entsprechenden Code in pgsql zu schreiben Vielen Dank

Antwort

0

Sie benötigen keine eigene Funktion zu schreiben - Postgres hat bereits eine integrierte Funktion für das: 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; 

Wenn Sie einen Text sind vorbei, aber Sie müssen das auf eine ganze Zahl zu vergleichen, müssen Sie das resultierende Array werfen:

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; 

Aber: das ist ein ziemlich hässlich Abhilfe das ist nur in Oracle notwendig, weil es keine echten Arrays in SQL unterstützt (nur in PL/SQL).

In Postgres wäre es viel besser sein, eine Reihe von ganzen Zahlen (z.B. :types eine int[] machen) direkt zu übergeben. Dann würde kein Parsing oder Gießen erforderlich sein:

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

Dank, und was ist mit den:? Typen Argument der int ist, was soll ich tun, Sorry, ich bin neu –

+0

pgsql Sie haben nicht gezeigt, wie Sie diese Anweisung ausführen . Aber ich nehme an, dass der Parameter ': types', den Sie dort verwenden, ein 'text'-Wert ist (weil Ihre Funktion dort auch' text' erwartet). Wie und wo ersetzen Sie diese Parameter? Was war der Datentyp von ': types' in Oracle? –

+0

der Datentyp von update_type in Oracle ist int –