2016-03-29 14 views
0

Zunächst einmal habe ich in Oracle eine Prozedur, die einen Clob zurückgibt. Dieser Clob enthält eine JSON-Zeichenfolge, die ich mit pljson von einem SQL-Select erstellt habe. So:Postgres: Return json cob

procedure xyz 
(
    o_json out clob 
) 
is 
    m_json_list json_list := json_list; 
    m_json_temp json; 
begin 
    for cs in (select id, name, birthday from profile)loop 
     m_json_temp := json; 

     m_json_temp.put('id', cs.id); 
     m_json_temp.put('name', cs.name); 
     m_json_temp.put('birthday', cs.birthday); 

     m_json_list.add(m_json_temp); 
    end loop; 
    o_json := convertToClob(m_json_list); 
end xyz; 

Jetzt möchte ich das gleiche Ergebnis mit einer Postgres-Datenbank achive. Das einzige, was ich gefunden habe, ist, dass ich eine Tabelle mit einer Hülle habe, die den Typ 'JSON' hat und das ganze JSON enthält. Das ist nicht was ich suche.

Kann mir jemand ein Beispiel geben, wie man dieses Szenario in Postgresql erreichen kann?

EDIT: Hier ist ein Beispiel für eine innere Verknüpfung:

procedure xyz 
(
    o_json out clob 
) 
is 
    m_json_list json_list := json_list; 
    m_json_temp json; 
begin 
    for cs in (select ppf.id, ppf.name, ppf.birthday, ott.info from profile ppf inner join other_table ott on ott.ott_id = ppf.id)loop 
     m_json_temp := json; 

     m_json_temp.put('id', cs.id); 
     m_json_temp.put('name', cs.name); 
     m_json_temp.put('birthday', cs.birthday); 
     m_json_temp.put('info', cs.info); 

     m_json_list.add(m_json_temp); 
    end loop; 
    o_json := convertToClob(m_json_list); 
end xyz; 
+0

Warum nicht, was ist der Unterschied? Postgres behandelt "lange" Felder (json in Ihrem Fall), ähnlich wie Oracle Clobs behandelt. Also, was genau willst du? –

+0

Ich möchte Joins mit anderen Tabellen erstellen. – Lee

+0

Und wie machst du JSON Joins bei Oracle? Können Sie ein Beispiel geben? –

Antwort

1

Also, Sie sind nach einer Möglichkeit, ein JSON-Array aus einer Abfrage zu konstruieren.

einen Tisch und einige Testdaten Gegeben:

postgres=# select row_to_json(p.*) from profile p; 
        row_to_json     
------------------------------------------------ 
{"id":1,"name":"John","birthday":"1986-03-29"} 
{"id":2,"name":"Jack","birthday":"1991-03-29"} 
(2 rows) 

Und dann aggregieren diese json Objekte in einem Array:

postgres=# create table profile(id serial, name text, birthday date); 
CREATE TABLE 
postgres=# insert into profile(name, birthday) values('John', current_date - interval '30 years'); 
INSERT 0 1 
postgres=# insert into profile(name, birthday) values('Jack', current_date - interval '25 years'); 
INSERT 0 1 

Sie Zeilen als JSON-Objekte wie folgt darstellen

postgres=# select json_agg(row_to_json(p.*)) from profile p; 
              json_agg            
-------------------------------------------------------------------------------------------------- 
[{"id":1,"name":"John","birthday":"1986-03-29"}, {"id":2,"name":"Jack","birthday":"1991-03-29"}] 
(1 row) 

Noch einfacher, Sie können nur eine Aggregation verwenden und es wird alle Konvertierungen für yo tun u:

postgres=# select json_agg(p.*) from profile p; 
        json_agg      
--------------------------------------------------- 
[{"id":1,"name":"John","birthday":"1986-03-29"}, + 
    {"id":2,"name":"Jack","birthday":"1991-03-29"}] 
(1 row) 

(Nie + Hinweisschild, es ist kein Teil von json.)