ORACLE : renaming a key in a json field by oracle - sql

I have a table in which in one of the columns there is a json data as below
COLUMN NAME : JSON_CODE
{
"DataElaborazione" : "20220718",
"DataMovimento" : "20220531",
"DataRiferimento" : "20220715",
}
I want to rename DataRiferimento to datarif
expected result
COLUMN NAME : JSON_CODE
{
"DataElaborazione" : "20220718",
"DataMovimento" : "20220531",
"datarif" : "20220715",
}
see image for structure
thank you

You can use JSON_TRANSFORM function:
select JSON_TRANSFORM('{ "DataElaborazione" : "20220718", "DataMovimento" : "20220531", "DataRiferimento" : "20220715" }',
RENAME '$.DataRiferimento' = 'datarif')
from dual
Or JSON_OBJECT_T:
declare
v_json JSON_OBJECT_T;
begin
v_json := JSON_OBJECT_T('{ "DataElaborazione" : "20220718", "DataMovimento" : "20220531", "DataRiferimento" : "20220715" }');
v_json.rename_key('DataRiferimento', 'datarif');
dbms_output.put_line(v_json.stringify);
end;
You can create function and use it in your query:
create or replace function renameJsonKey(p_json IN VARCHAR2,
p_old_key IN VARCHAR2,
p_new_key IN VARCHAR2) return VARCHAR2
RESULT_CACHE RELIES_ON (your_table)
is
v_json JSON_OBJECT_T;
begin
v_json := JSON_OBJECT_T(p_json);
v_json.rename_key(p_old_key, p_new_key);
return v_json.stringify;
end;
select renameJsonKey(col, 'DataRiferimento', 'datarif') from your table

Related

Create JSON from XML - JSON_AGG OUTPUT PROBLEM

I have a problem with converting XML content to JSON format (with plain oracle select statement), where more then 1 sub level of data is present in the original XML - with my code the result of level 2+ is presented as string and not as JSON_OBJECT. Please, could someone tell me, where is fault in my code or what I'm doing wrong:
source:
<envelope>
<sender>
<name>IZS</name>
<country>SU</country>
<address>LOCATION 10B</address>
<address>1000 CITY</address>
<sender_identifier>SU46794093</sender_identifier>
<sender_address>
<sender_agent>SKWWSI20XXX</sender_agent>
<sender_mailbox>SI56031098765414228</sender_mailbox>
</sender_address>
</sender>
</envelope>
transformation select statement:
WITH SAMPLE AS (SELECT XMLTYPE ('
<envelope>
<sender>
<name>IZS</name>
<country>SU</country>
<address>LOCATION 10B</address>
<address>1000 CITY</address>
<sender_identifier>SU46794093</sender_identifier>
<sender_address>
<sender_agent>SKWWSI20XXX</sender_agent>
<sender_mailbox>SI56031098765414228</sender_mailbox>
</sender_address>
</sender>
</envelope>') XMLDOC FROM DUAL)
SELECT JSON_SERIALIZE (
JSON_OBJECT (
KEY 'envelope' VALUE
JSON_OBJECTAGG (
KEY ID_LEVEL1 VALUE
CASE ID_LEVEL1
WHEN 'sender' THEN
( SELECT JSON_OBJECTAGG (
KEY ID_LEVEL2 VALUE
CASE ID_LEVEL2
WHEN 'sender_address' THEN
( SELECT JSON_OBJECTagg (KEY ID_LEVEL22 VALUE TEXT_LEVEL22)
FROM XMLTABLE ('/sender/sender_address/*'
PASSING XML_LEVEL2
COLUMNS ID_LEVEL22 VARCHAR2 (128) PATH './name()',
TEXT_LEVEL22 VARCHAR2 (128) PATH './text()'
)
)
ELSE
TEXT_LEVEL2
END)
FROM XMLTABLE ('/sender/*'
PASSING XML_LEVEL2
COLUMNS ID_LEVEL2 VARCHAR2 (1024) PATH './name()',
TEXT_LEVEL2 VARCHAR2 (1024) PATH './text()'
)
)
ELSE
'"' || TEXT_LEVEL1 || '"'
END FORMAT JSON)
) PRETTY
)JSON_DOC
FROM SAMPLE, XMLTABLE ('/envelope/*'
PASSING XMLDOC
COLUMNS ID_LEVEL1 VARCHAR2 (1024) PATH './name()',
TEXT_LEVEL1 VARCHAR2 (1024) PATH './text()',
XML_LEVEL2 XMLTYPE PATH '.'
);
wrong result:
{
"envelope" :
{
"sender" :
{
"name" : "IZS",
"country" : "SU",
"address" : "LOCATION 10B",
"address" : "1000 CITY",
"sender_identifier" : "SU46794093",
"sender_address" : "{\"sender_agent\":\"SKWWSI20XXX\",\"sender_mailbox\":\"SI56031098765414228\"}"
}
}
}
wrong part:
***"sender_address" : "{\"sender_agent\":\"SKWWSI20XXX\",\"sender_mailbox\":\"SI56031098765414228\"}"***
For the level 1 text you're wrapping the value in double-quotes and specifying format json; you aren't doing that for level 2. If you change:
ELSE
TEXT_LEVEL2
END
to:
ELSE
'"' || TEXT_LEVEL2 || '"'
END FORMAT JSON)
then the result is:
{
  "envelope" :
  {
    "sender" :
    {
      "name" : "IZS",
      "country" : "SU",
      "address" : "LOCATION 10B",
      "address" : "1000 CITY",
      "sender_identifier" : "SU46794093",
      "sender_address" :
      {
        "sender_agent" : "SKWWSI20XXX",
        "sender_mailbox" : "SI56031098765414228"
      }
    }
  }
}
fiddle
The problem is that you need kind of conditional "FORMAT JSON" in the "SELECT JSON_OBJECTAGG ( KEY ID_LEVEL2 VALUECASE ID_LEVEL2": when the ID_LEVEL2 is 'sender_address' but not in the ELSE part, but the syntax requires you put after the END of CASE, and of course this fails for the "ELSE TEXT_LEVEL2" part.

How to update XML attribute in clob Oracle using XMLQuery

Oracle table name: SR_DATA;
Table field name: XMLDATA type CLOB;
Field value:
<module xmlns="http://www.mytest.com/2008/FMSchema">
<tmEsObjective modelCodeScheme="A" modelCodeSchemeVersion="01" modelCodeValue="ES_A"></tmEsObjective>
</module>
I need to update the value of the attribute "modelCodeValue" into ES_B.
This is the code:
UPDATE SR_DATA
SET XMLDATA =
XMLQuery('copy $i := $p1 modify
((for $j in $i/module/tmEsObjective/#modelCodeValue
return replace value of node $j with $p2))
)
return $i'
PASSING XMLType(REPLACE(xmldata, 'xmlns="http://www.mytest.com/2008/FMSchema"', '')) AS "p1",
'ES_B' AS "p2"
RETURNING CONTENT);
This code returns the error code: ORA-00932: inconsistent datatypes: expected CLOB got -
Use getclobval() like this:
UPDATE SR_DATA
SET XMLDATA =
XMLTYPE.GETCLOBVAL(XMLQuery('copy $i := $p1 modify
((for $j in $i/module/tmEsObjective/#modelCodeValue
return replace value of node $j with $p2))
return $i'
PASSING XMLType(REPLACE(xmldata, 'xmlns="http://www.mytest.com/2008/FMSchema"', '')) AS "p1",
'ES_B' AS "p2"
RETURNING CONTENT ));

plsql cannot use variables in sql select

I am getting very fed up of APEX in recent days, i am trying to run a interactive report within application express and cannot seem to use variables i declare within my sql queries. Below is the error, i also included the code that works. the get_longlat() function is tested and works it returns a SDO_GEOMETRY data type.
ORA-06550: line 7, column 12: PLS-00306: wrong number or types of
arguments in call to '||' ORA-06550: line 7, column 1: PL/SQL:
Statement ignored
DECLARE
l_query VARCHAR2(4096);
l_location SDO_GEOMETRY := get_longlat(v('P2_POSTCODE'));
BEGIN
l_query := '
SELECT
"VENUE_ID",
"VENUE_NAME",
"CITY",
"VENUE_NO",
"POSTCODE",
dbms_lob.getlength("THUMBNAIL") "THUMBNAIL",
"DESCRIPTION",
SDO_GEOM.SDO_DISTANCE(G_LOCATION, ' || l_location || ',0.005,''unit=mile'') "G_LOCATION"
FROM "GAMEVENUE"
WHERE (SDO_WITHIN_DISTANCE(G_LOCATION,' || l_location || ',''distance=250 unit=mile'') = ''TRUE'');';
RETURN l_query;
This works
l_query := '
SELECT
"VENUE_ID",
"VENUE_NAME",
"CITY",
"VENUE_NO",
"POSTCODE",
dbms_lob.getlength("THUMBNAIL") "THUMBNAIL",
"DESCRIPTION",
SDO_GEOM.SDO_DISTANCE(G_LOCATION, MDSYS.SDO_GEOMETRY(2001,4326,SDO_POINT_TYPE(-4.1530439,50.371089,NULL),NULL,NULL),0.005,''unit=mile'') "G_LOCATION"
FROM "GAMEVENUE"
WHERE (SDO_WITHIN_DISTANCE(G_LOCATION,SDO_GEOMETRY(2001,4326,SDO_POINT_TYPE(-4.1530439,50.371089,null),null,null),''distance=250 unit=mile'') = ''TRUE'')';
Because SDO_GEOMETRY is not of type char or number( implicitly considerable as char ), you can't use it in direct concetenation.
Try this instead :
l_query := '
SELECT
"VENUE_ID",
"VENUE_NAME",
"CITY",
"VENUE_NO",
"POSTCODE",
dbms_lob.getlength("THUMBNAIL") "THUMBNAIL",
"DESCRIPTION",
SDO_GEOM.SDO_DISTANCE(G_LOCATION, MDSYS.SDO_GEOMETRY('||l_sdo_gtype||','||l_sdo_srid||',MDSYS.SDO_POINT_TYPE('||l_longitude||','||l_latitude||',NULL),NULL,NULL),0.005,''unit=mile'') "G_LOCATION"
FROM "GAMEVENUE"
WHERE (SDO_WITHIN_DISTANCE(G_LOCATION,SDO_GEOMETRY('||l_sdo_gtype||','||l_sdo_srid||',SDO_POINT_TYPE('||l_longitude||','||l_latitude||',null),null,null),''distance=250 unit=mile'') = ''TRUE'')';
where l_sdo_gtype,l_sdo_srid, l_longitude, l_latitude are of type number;

how to upload any file from local machine to database server file system using SQL developer

Here is my Code:
I need to upload a file from the local machine to the file system
Also, i got an error when opening the file attached in the oracle application (ORA-01403: no data found
FRM-40735: WHEN-BUTTON-PRESSED trigger raised unhandled exception
ORA-01403.)
CREATE OR REPLACE DIRECTORY in_file_loc AS '/oracle/TEST';
CREATE TABLE test_files (p_id NUMBER,
pl_name VARCHAR2(100),
pl_pict BLOB);
DECLARE
x_blob BLOB;
fils BFILE := BFILENAME ('IN_FILE_LOC', 'aa.pdf');
blob_length INTEGER;
BEGIN
-- Obtain the size of the blob file
DBMS_LOB.fileopen (fils, DBMS_LOB.file_readonly);
blob_length := DBMS_LOB.getlength (fils);
DBMS_LOB.fileclose (fils);
-- Insert a new record into the table containing the
-- filename you have specified and a LOB LOCATOR.
-- Return the LOB LOCATOR and assign it to x_blob.
INSERT INTO test_files (p_id,pl_name,pl_pict)
VALUES (7, 'aa.pdf', EMPTY_BLOB ())
RETURNING pl_pict
INTO x_blob;
-- Load the file into the database as a BLOB
DBMS_LOB.OPEN (fils, DBMS_LOB.lob_readonly);
DBMS_LOB.OPEN (x_blob, DBMS_LOB.lob_readwrite);
DBMS_LOB.loadfromfile (x_blob, fils, blob_length);
-- Close handles to blob and file
DBMS_LOB.CLOSE (x_blob);
DBMS_LOB.CLOSE (fils);
COMMIT;
-- Confirm insert by querying the database
-- for LOB length information and output results
blob_length := 0;
SELECT DBMS_LOB.getlength (pl_pict)
INTO blob_length
FROM test_files
WHERE pl_name = 'aa.pdf';
DBMS_OUTPUT.put_line ('Successfully inserted BLOB ''' ||'aa.pdf' || ''' of size ' || blob_length || ' bytes.');
END;
DECLARE
TYPE result_set_type IS REF CURSOR;
l_name VARCHAR2 (100);
l_doc_size NUMBER;
l_result_set_curr result_set_type;
x_access_id NUMBER;
x_file_id NUMBER;
p_file_name VARCHAR2 (100) := 'aa.pdf';
PROCEDURE load_file_details (p_name IN VARCHAR2, result_set_curr OUT result_set_type)
AS
l_error VARCHAR2 (2000);
BEGIN
INSERT INTO fnd_lobs_document
(NAME, mime_type, doc_size, content_type, blob_content)
SELECT pl_name, 'application/pdf', DBMS_LOB.getlength (pl_pict), 'BINARY', '(BLOB)'
FROM test_files
WHERE pl_name = 'aa.pdf';
OPEN result_set_curr FOR
SELECT blob_content
FROM fnd_lobs_document
WHERE NAME = p_name;
EXCEPTION
WHEN OTHERS
THEN
NULL;
l_error := 'LOAD_FILE_DETAILS - OTHERS' || SUBSTR (SQLERRM, 2000);
DBMS_OUTPUT.put_line (l_error);
END load_file_details;
PROCEDURE upload_file (v_filename IN VARCHAR2, x_access_id OUT NUMBER, x_file_id OUT NUMBER)
AS
v_access_id NUMBER;
v_file_id NUMBER;
x_errbuf VARCHAR2 (200);
BEGIN
v_access_id := fnd_gfm.authorize (NULL);
x_access_id := v_access_id;
DBMS_OUTPUT.put_line ('Access id :' || v_access_id);
-- The function fnd_gfm.confirm_upload return the file id
v_file_id :=
fnd_gfm.confirm_upload (access_id => v_access_id
, file_name => v_filename
, program_name => 'TEST'
, program_tag => 'TEST'
, expiration_date => NULL
, LANGUAGE => 'US'
, wakeup => TRUE
);
x_file_id := v_file_id;
DBMS_OUTPUT.put_line ('File id :' || x_file_id);
EXCEPTION
WHEN OTHERS
THEN
x_errbuf := 'Procedure upload_file errored out with the following error : ' || SQLERRM;
DBMS_OUTPUT.put_line (x_errbuf);
END upload_file;
BEGIN
fnd_global.apps_initialize (0, 20634, 401);
load_file_details (p_name => p_file_name, result_set_curr => l_result_set_curr);
upload_file (v_filename => p_file_name, x_access_id => x_access_id, x_file_id => x_file_id);
COMMIT;
END;
DECLARE
TYPE result_set_type IS REF CURSOR;
l_name VARCHAR2 (100);
l_doc_size NUMBER;
l_result_set_curr result_set_type;
x_access_id NUMBER;
x_file_id NUMBER;
p_file_name VARCHAR2 (100) := 'aa.pdf';
PROCEDURE attach_file (p_access_id IN NUMBER, p_file_Id in number, p_filename IN VARCHAR2)
IS
BEGIN
fnd_webattch.add_attachment (seq_num => 900
,category_id => 1
,document_description => 'Test1'
,datatype_id => 5
,text => NULL
,file_name => p_filename
,url => NULL
,function_name => 'INVIDITM'
,entity_name => 'MTL_SYSTEM_ITEMS'
,pk1_value => 85 --organization_id
,pk2_value => 10 --Inventory_item_Id
,pk3_value => NULL
,pk4_value => NULL
,pk5_value => NULL
,media_id => p_file_id
,user_id => 0
,usage_type => 'O'
);
DBMS_OUTPUT.put_line ('File Attached!');
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line ('error in loading the attachement');
END attach_file;
BEGIN
fnd_global.apps_initialize (0, 20634, 401);
attach_file (p_access_id => x_access_id, p_file_id=>x_file_id,p_filename => p_file_name);
COMMIT;
END;
I believe we are mixing context here. I assume Local machine here means the machine that you build the PL/SQL - only issue here is when you run it runs within the server context. So i would either FTP the file to the server or load your file content to an oracle table in a different way.

SQL stored procedure compile error: wrong number or types of arguments in call

I have created this stored procedure. However I get this error
ORA-06550:line 14 column 3: PLS-00306:
wrong number or types of arguments in
call to 'CHALLENGEUPDATE' when the
values passed are CUSTID
:='400800500' ; POLICYNAME :=
'POLICY'; CHALLENGEID :=
'CHALLENGEID1'; PINPOSITIONS :=
'PINPOS';
The code is:
create or replace
procedure ChallengeUpdate
(
CustID IN SEC_CHALLENGE.CUSTOMERID%TYPE,
PolicyName IN SEC_CHALLENGE.POLICY_NAME%TYPE,
ChallengeId IN SEC_CHALLENGE.CHALLENGE_ID%TYPE,
PinPositions IN SEC_CHALLENGE.PINPOSITIONS%TYPE,
o_result OUT INTEGER,
o_code OUT INTEGER,
o_c OUT INTEGER
)
as
c integer;
begin
select count(*) into c from SEC_CHALLENGE xyz where xyz.CUSTOMERID=CustID and
xyz.POLICY_NAME=PolicyName;
if c = 0 then
INSERT INTO SEC_CHALLENGE(CUSTOMERID,CHALLENGE_ID,PINPOSITIONS,POLICY_NAME)
VALUES (CustID,ChallengeId, PinPositions, PolicyName);
else
UPDATE RBSSEC_CHALLENGE
SET CHALLENGE_ID=ChallengeId, PINPOSITIONS=PinPositions
WHERE CUSTOMERID=CustID and POLICY_NAME = PolicyName;
end if;
commit;
o_result:=0;
o_c:=c;
exception when others then
o_result:=-1;
o_c:=c;
rollback;
o_code :=SQLCODE;
end ChallengeUpdate;
The SEC_CHALLENGE table is this
CREATE TABLE "MUJEEB"."RBSSEC_CHALLENGE"
(
"CUSTOMERID" VARCHAR2(9 BYTE) NOT NULL ENABLE,
"CHALLENGE_ID" VARCHAR2(50 BYTE),
"PINPOSITIONS" VARCHAR2(20 BYTE) NOT NULL ENABLE,
"POLICY_NAME" VARCHAR2(50 BYTE) NOT NULL ENABLE,
CONSTRAINT "RBSSEC_CHALLENGE1_PK" PRIMARY KEY ("CHALLENGE_ID")
)
here is how I am running it:
DECLARE CUSTID VARCHAR2(9);
POLICYNAME VARCHAR2(50);
CHALLENGEID VARCHAR2(50);
PINPOSITIONS VARCHAR2(20);
O_RESULT NUMBER;
O_CODE NUMBER;
BEGIN CUSTID :='400800500' ;
POLICYNAME := 'POLICY';
CHALLENGEID := 'CHALLENGEID1';
PINPOSITIONS := 'PINPOS';
CHALLENGEUPDATE( CUSTID => CUSTID, POLICYNAME => POLICYNAME, CHALLENGEID => CHALLENGEID, PINPOSITIONS => PINPOSITIONS, O_RESULT => O_RESULT, O_CODE => O_CODE );
DBMS_OUTPUT.PUT_LINE('O_RESULT = ' || O_RESULT);
DBMS_OUTPUT.PUT_LINE('O_CODE = ' || O_CODE);
END;
If you use output parameters, marked with the "out" keyword, you must pass a variable for that parameter. Output params are not optional. From the error, I gather that you have not done so.
if you are using toad, try it this way:
var o_result integer;
var ocode integer;
var oc integer;
exec ChallengeUpdate ('400800500', 'POLICY', 'CHALLENGEID1', 'PINPOS', :o_result, :ocode, :oc );
print o_result;
print ocode ;
print oc;
From the declaration:
procedure ChallengeUpdate
(
CustID IN SEC_CHALLENGE.CUSTOMERID%TYPE,
PolicyName IN SEC_CHALLENGE.POLICY_NAME%TYPE,
ChallengeId IN SEC_CHALLENGE.CHALLENGE_ID%TYPE,
PinPositions IN SEC_CHALLENGE.PINPOSITIONS%TYPE,
o_result OUT INTEGER,
o_code OUT INTEGER,
o_c OUT INTEGER
) as
Your call to CHALLENGEUPDATE happens to miss an output variable for o_c OUT INTEGER. You may try the bellow:
exec CHALLENGEUPDATE(
CUSTID => CUSTID
, POLICYNAME => POLICYNAME
, CHALLENGEID => CHALLENGEID
, PINPOSITIONS => PINPOSITIONS
, O_RESULT => O_RESULT
, O_CODE => O_CODE
, O_C => O_C_tobedeclared);