How to read a field Oracle containing XML - sql

A table exists in the environment of production with the following structure:
CREATE TABLE gold_dwh_reload (
msisdn NUMBER(13,0) NOT NULL,
recharge_date TIMESTAMP(6) NOT NULL,
impacted_balances VARCHAR2(4000) NULL,
lc_state VARCHAR2(5) NOT NULL)
TABLESPACE sopfun_tab
NOCOMPRESS
/
A normal consultation would the following result by example:
MSISDN RECHARGE_DATE IMPACTED_BALANCES LC_STATE
584124723950 29.04.15 13:23:38.000 <balance><name>B_LPP_Bs_Main</name><label></label><before>697.21429</before><after>797.21429</after><amount>100</amount><start></start><end></end><unit>Bs</unit></balance><balance><name>B_LPP_KB_National</name><label>PA_Adjustment</label><before>0</before><after>10240</after><amount>10240</amount><start>29042015000000</start><end>29052015000000</end><unit>Kbytes</unit></balance><balance><name>B_LSP_Bs_Promotions</name><label>PA_Adjustment</label><before>0</before><after>25</after><amount>25</amount><start>29042015000000</start><end>29052015000000</end><unit>Bs</unit></balance> ACT
But i need to break the IMPACTED_BALANCES field in columns. Anyone know how I do it?

This is typically done using XMLTable
select
msisdn, recharge_date,
x_name, x_label, x_before, x_after, x_amount,
to_date(x_start, 'DDMMYYYYHH24MISS') x_start,
to_date(x_end, 'DDMMYYYYHH24MISS') x_end,
x_unit,
lc_state
from gold_dwh_reload
cross join
xmltable('/balances/balance'
passing xmltype('<balances>'||impacted_balances||'</balances>')
columns
x_name path '/balance/name',
x_label path '/balance/label',
x_before number path '/balance/before',
x_after number path '/balance/after',
x_amount number path '/balance/amount',
x_start path '/balance/start',
x_end path '/balance/end',
x_unit path '/balance/unit'
);
Here's a SQL Fiddle.
Mixing SQL and XML is powerful but creates many potential type safety issues. A single invalid date, number, or XML file will crash the whole query. The string in your example is not valid XML, that's why I concatenated another tag to the beginning and end.

Related

External Table I created pulls partial characters from string column and adds numbers to integer column

CREATE TABLE SKILL_LEVEL_DIM_X
(
SKILL_LEVEL_ID INTEGER
,SKILL_LEVEL_NAME VARCHAR(50)
,SKILL_LEVEL_DESCRIPTION VARCHAR(500)
-- ,CRE_DTTM DATE NOT NULL DEFAULT SYSDATE
-- ,UPD_DTTM DATE NOT NULL DEFAULT SYSDATE
)
ORGANIZATION EXTERNAL
(
TYPE ORACLE_LOADER
DEFAULT DIRECTORY ABC_DB_SKILLS
ACCESS PARAMETERS (
RECORDS DELIMITED BY NEWLINE
SKIP 1
FIELDS TERMINATED BY ','
LRTRIM
MISSING FIELD VALUES ARE NULL
REJECT ROWS WITH ALL NULL
FIELDS (
SKILL_LEVEL_ID INTEGER
,SKILL_LEVEL_NAME CHAR(50)
,SKILL_LEVEL_DESCRIPTION CHAR(500)
)
)
LOCATION ('SKILL_LEVEL_DIM.csv')
);
I created an external table in Oracle SQL to pull data from a .csv file. After I create the staging table and query from it, SKILL_LEVEL_ID is '1' in the .csv file to '1094855729' in the external table and 'BASIC' in the.csv file to 'SIC' in the external table.
csv file
export
when you use INTEGER, it means the data is in binary format, not numeric characters like you have. Use CHAR and see what it gives. And for the column description, use VARCHAR2 and not VARCHAR.

Failed to execute query. Error: String or binary data would be truncated in table xdbo.user_info', column 'uid'

I have problem inserting values in my SQL server database on Azure, I am getting the following error:
Failed to execute query. Error: String or binary data would be truncated in table 'dummy_app.dbo.user_info', column 'uid'. Truncated value: 'u'.
The statement has been terminated.
I don't understand where I am wrong, I just created the server, and I am trying to experiment but cant fix this.
if not exists (select * from sysobjects where name='user_info' and xtype='U')
create table user_info (
uid varchar unique,
name varchar,
email varchar
)
go;
INSERT INTO dbo.user_info(uid, name, email) VALUES('uids', 'name', 'email') go;
Creating the table works fine, the only thing that doesn't work is the second command INSERT
I suspect that the reason is that you haven't defined a lenght for varchar and it defaults to 1 as length. Therefore your value gets truncated.
Set a varchar length to something like varchar(200) and you should be good to go.
This looks like the fact that the CREATE portion of your procedure for the table doesn't include a length of varchar, so you'd have to specify a length such as varchar(50) since the default is 1. Refer to the official MS docs in the link, in the remarks.
docs.miscrosoft.com
Also, here is the syntax for the CREATE TABLE in Azure which might be helpful as well.
Syntax of Azure CREATE TABLE

Extract data from file name and store it in a table using stored procedure

I have different types of files (pdf, csv, doc, txt) in a directory.
Name of the files are something like this:
John.Doe.19900101.TX.pdf //FirstName.LastName.DOB.StateOfResidence
Bruce.Banner.19700101.PA.doc
Steve.Rodgers.19760101.AR.csv
Tony.Stark.19901210.CA.txt
How to write a stored procedure in Oracle to read the files in a directory and extract FirstName, LastName, DOB, State and store it in a table in appropriate columns?
Ex: For the file John.Doe.19900101.TX.pdf, data should be extracted like this:
John in FirstName column
Doe in LastName column
19900101 in DOB column
TX in State column
whole file in CLOB column
You will have to work at the OS level to gather filenames from OS directory. Considering you are trying to get the information from Unix flavors; Following URL will help you grab the file listing into a table and even a view.
Code that you need is :
--drop directory SCRIPT_TEMP_DIR;
CREATE DIRECTORY SCRIPT_TEMP_DIR AS '/home/oracle/oracle_scripts'
;
GRANT EXECUTE ON DIRECTORY SCRIPT_TEMP_DIR TO USER_NAME
; -- Here USER_NAME will be your SCHEMA/USER NAME
drop table USER_NAME.home_directory purge;
CREATE TABLE USER_NAME.home_directory
(
filerecord VARCHAR2(15),
flink VARCHAR2(2),
fowner VARCHAR2(6),
fgroup VARCHAR2(8),
fsize VARCHAR2(32),
fdate_part1 VARCHAR2(16),
fdate_part2 VARCHAR2(16),
fdate_year_or_time VARCHAR2(16),
fname VARCHAR2(255)
)
ORGANIZATION EXTERNAL
(
TYPE ORACLE_LOADER
DEFAULT DIRECTORY SCRIPT_TEMP_DIR
ACCESS PARAMETERS
(
records delimited by newline
preprocessor SCRIPT_TEMP_DIR:'listing.sh'
fields terminated by whitespace
(
filerecord ,
flink ,
fowner ,
fgroup ,
fsize ,
fdate_part1 ,
fdate_part2 ,
fdate_year_or_time ,
fname
)
)
LOCATION ('listing.sh')
)
REJECT LIMIT UNLIMITED;
Once this is done, you just need to select from above table created.
SELECT *
FROM USER_NAME.home_directory;
Later you can apply substr/instr functions to split info. You may also use regex function to get the requried information.
SELECT fname,
regexp_substr(fname, '[^.]+', 1, 1) part1,
regexp_substr(fname, '[^.]+', 1, 2) part2,
regexp_substr(fname, '[^.]+', 1, 3) part3
FROM USER_NAME.home_directory;
And this gives you :
The required URL to follow is here
The code pasted above was modified where you need to change USER_NAME as well while granting permissions on directory.

Using JSON_TABLE on ORACLE DB

create table JSON_TAB (JSON_VAL CLOB);
-- tried to add the constraint like this CONSTRAINT JTE_CK check (JSON_VAL is json) butt says it expects null so I didn't create it for now.
I am trying to execute the statement
SELECT JT.Ime, JT.Broj, JT.Pozicija
FROM JSON_TAB JTE,
JSON_TABLE (JTE.JSON_VAL, '$.players[*]'
COLUMNS (Ime VARCHAR2(20) PATH '$.name',
Broj NUMBER PATH '$.number',
Pozicija VARCHAR2 PATH '$.position')) JT ;
and receiving an error SQL command not properly ended.
JSON_VAL is a CLOB!
Is it a problem with syntax, data type or something else?
I took the example from youtube tutorial and entered it manually.
ORACLE version is 12.1.0.2.0.

ORA-29913: error in executing ODCIEXTTABLEOPEN callout

I am creating external table using hr schema but i get errors
"ORA-29913: error in executing ODCIEXTTABLEOPEN callout ORA-29400:
data cartridge error KUP-00554: error encountered while parsing access
parameters KUP-01005: syntax error: found "missing": expecting one of:
"column, (" KUP-01007: at line 4 column 3
29913. 00000 - "error in executing %s callout"
*Cause: The execution of the specified callout caused an error.
*Action: Examine the error messages take appropriate action."
----------------My Code-------------------
create directory ex_tab as 'C:\My Works\External Table';
create table strecords (
st_id number(4),
st_name varchar(10),
schl_name varchar(5),
st_city varchar(15),
st_year number(4)
)
ORGANIZATION EXTERNAL
(TYPE oracle_loader
DEFAULT DIRECTORY ex_tab
ACCESS PARAMETERS
(
RECORDS DELIMITED BY newline
FIELDS TERMINATED BY ','
REJECT ROWS WITH ALL NULL FIELDS
MISSING FIELDS VALUES ARE NULL
(
st_id number(4),
st_name char(10),
schl_name char(5),
st_city char(15),
st_year number(4)
)
)
LOCATION ('strecords.txt')
);
desc strecords;
select * from strecords;
This is my code, please check it and review it.
You have several issues here. The immediate one causing your problem is that you have the clauses in the wrong order, but you also have MISSING FIELDS instead of MISSING FIELD:
...
ACCESS PARAMETERS
(
RECORDS DELIMITED BY newline
FIELDS TERMINATED BY ','
MISSING FIELD VALUES ARE NULL
REJECT ROWS WITH ALL NULL FIELDS
(
...
Then your field list contents have invalid data types for that part of the statement; you can just omit that entirely in this case as those match the table column definition.
If no field list is specified, then the fields in the data file are assumed to be in the same order as the fields in the external table.
So you can simplify it to:
create table strecords (
st_id number(4),
st_name varchar(10),
schl_name varchar(5),
st_city varchar(15),
st_year number(4)
)
ORGANIZATION EXTERNAL
(TYPE oracle_loader
DEFAULT DIRECTORY ex_tab
ACCESS PARAMETERS
(
RECORDS DELIMITED BY newline
FIELDS TERMINATED BY ','
MISSING FIELD VALUES ARE NULL
REJECT ROWS WITH ALL NULL FIELDS
)
LOCATION ('strecords.txt')
);
Some operations needed against an Oracle Bug called
Select From External Table Returns Errors ORA-29913 ORA-29400 KUP-554 KUP-1005 (Doc ID 302672.1)
When creating an external table, the access parameters should be specified in the following order:
Comments
Record Format Info
Field definitions
Specify Comments, Record Format Info and Field definitions in the correct order. Even inside the Record format Info, 'Records delimited by ...' clause should come before any other clause.
For more information, refer to the access_parameters clause.