如何使用SQL Developer提供的“技巧”将每个表从特定用户转换为JSON格式

问题描述 投票:0回答:1

我想将所有表从特定用户转换为JSON(或XML)格式。我读过有关SQL Developer提到的“技巧”。

换句话说,我已经开始创建具有两个参数的过程:

  • p_format:格式(在我的情况下为“ json”)
  • p_user:用户名

作为IDE,我使用Oracle SQL Developer,而我的数据库是Oracle XE数据库

首先,该过程循环遍历给定用户的所有表,并且在循环中应执行以下操作:

SELECT /*p_format*/ * FROM p_user || '.' || table

不幸的是,如上所述,我无法使用此SELECT语句。我需要使用命令EXECUTE IMMEDIATE <Statement>

我面临的下一个问题是:我想输出EXECUTE IMMEDIATE命令的结果。因此,我使用了命令EXECUTE IMMEDIATE <Statement> INTO <Variable>。编译完程序并执行之后,我遇到了以下错误:

"inconsistent datatypes: expected %s got %s"

这是我的程序代码:

CREATE OR REPLACE PROCEDURE EXPORT_TABLE_TO_FORMAT_FROM(p_format VARCHAR2, p_user VARCHAR2) IS
/***************************************************************************
        Author: 
        Class:  
        School: 
        Date:   

        Function - EXPORT_TABLE_TO_JSON_FROM(p_user):
        Displays the data of every table from a given User as JSON
        Parameter: p_user ... User
***************************************************************************/ 
v_tableData VARCHAR2(32767);
v_sqlStatement VARCHAR2(200);
BEGIN
  FOR tablerec IN (SELECT *
                   FROM   ALL_TABLES
                   WHERE OWNER = p_user)
  LOOP
    v_sqlStatement := 'SELECT /*' || p_format || '*/ * FROM ' || p_user || '.' || tablerec.TABLE_NAME;
    EXECUTE IMMEDIATE v_sqlStatement INTO v_tableData;

    DBMS_OUTPUT.PUT_LINE (v_sqlStatement);
  END LOOP;
END;

[您会看到我遍历给定用户的所有表,并使用p_formatp_user以及tablerec.TABLE_NAME创建了一个sql语句。

所需的结果应该看起来像这样:

{"results":[{"columns":[{"name":"COUNTRY_ID","type":"CHAR"},
{"name":"COUNTRY_NAME","type":"VARCHAR2"},{"name":"REGION_ID","type":"NUMBER"}],"items":
[
{"country_id":"AR","country_name":"Argentina","region_id":2},
{"country_id":"AU","country_name":"Australia","region_id":3},
{"country_id":"BE","country_name":"Belgium","region_id":1},
{"country_id":"BR","country_name":"Brazil","region_id":2},
{"country_id":"CA","country_name":"Canada","region_id":2},
{"country_id":"CH","country_name":"Switzerland","region_id":1},
{"country_id":"CN","country_name":"China","region_id":3},
{"country_id":"DE","country_name":"Germany","region_id":1},
{"country_id":"DK","country_name":"Denmark","region_id":1},
{"country_id":"EG","country_name":"Egypt","region_id":4},
{"country_id":"FR","country_name":"France","region_id":1},
{"country_id":"IL","country_name":"Israel","region_id":4},
{"country_id":"IN","country_name":"India","region_id":3},
{"country_id":"IT","country_name":"Italy","region_id":1},
{"country_id":"JP","country_name":"Japan","region_id":3},
{"country_id":"KW","country_name":"Kuwait","region_id":4},
{"country_id":"ML","country_name":"Malaysia","region_id":3},
{"country_id":"MX","country_name":"Mexico","region_id":2},
{"country_id":"NG","country_name":"Nigeria","region_id":4},
{"country_id":"NL","country_name":"Netherlands","region_id":1},
{"country_id":"SG","country_name":"Singapore","region_id":3},
{"country_id":"UK","country_name":"United Kingdom","region_id":1},
{"country_id":"US","country_name":"United States of America","region_id":2},
{"country_id":"ZM","country_name":"Zambia","region_id":4},
{"country_id":"ZW","country_name":"Zimbabwe","region_id":4}]}]}
sql json oracle plsql oracle-sqldeveloper
1个回答
2
投票

JSON提示特定于SQL Developer和SQLcl,而不直接针对数据库。因此,您需要在这些工具中运行整个过程。

最简单的方法是让脚本write可以运行的脚本,例如

spool /tmp/get_all_json.sql
select 'select /*json*/ * from '||table_name||';' 
from user_tables;
spool off
@/tmp/get_all_json.sql
© www.soinside.com 2019 - 2024. All rights reserved.