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

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.

Related

MACRO to create a table in SQL

Hi everyone thanks so much for taking the time to read this.
I'd like to create a macro in Teradata that will create a table from another table based on specific parameters.
My original table consists of three columns patient_id, diagnosis_code and Date_of_birth
......
I'd like to build a macro that would allow me to specify a diagnosis code and it would then build the table consisting of data of all patients with that diagnosis code.
My current code looks like this
Create Macro All_pats (diag char) as (
create table pats as(
select *
from original_table
where diag = :diagnosis_code;)
with data primary index (patid);
I cant seem to get this to work - any tips?
Thanks once again
Your code has a semicolon in a wrong place and a missing closing bracket:
Create Macro All_pats (diag char) as (
create table pats as
(
select *
from original_table
where diag = :diagnosis_code
) with data primary index (patid);
);
Edit:
Passing multiple values as a delimited list is more complicated (unless you use Dynamic SQL in a Stored Procedure):
REPLACE MACRO All_lpats (diagnosis_codes VARCHAR( 1000)) AS
(
CREATE TABLE pats AS
(
SELECT *
FROM original_table AS t
JOIN TABLE (StrTok_Split_To_Table(1, :diagnosis_codes, ',')
RETURNS (outkey INTEGER,
tokennum INTEGER,
token VARCHAR(20) CHARACTER SET Unicode)
) AS dt
ON t.diag = dt.token
) WITH DATA PRIMARY INDEX (patid);
);
EXEC All_lpats('111,112,113');
As the name implies StrTok_Split_To_Table splits a delimited string into a table. You might need to adust the delimiter and the length of the resulting token.

Hive: Partitioning by part of integer column

I want to create an external Hive table, partitioned by record type and date (year, month, day). One complication is that the date format I have in my data files is a single value integer yyyymmddhhmmss instead of the required date format yyyy-mm-dd hh:mm:ss.
Can I specify 3 new partition column based on just single data value? Something like the example below (which doesn't work)
create external table cdrs (
record_id int,
record_detail tinyint,
datetime_start int
)
partitioned by (record_type int, createyear=datetime_start(0,3) int, createmonth=datetime_start(4,5) int, createday=datetime_start(6,7) int)
row format delimited
fields terminated by '|'
lines terminated by '\n'
stored as TEXTFILE
location 'hdfs://nameservice1/tmp/sbx_unleashed.db'
tblproperties ("skip.header.line.count"="1", "skip.footer.line.count"="1");
If you want to be able to use MSCK REPAIR TABLE to add the partition for you based on the directories structure you should use the following convention:
The nesting of the directories should match the order of the partition columns.
A directory name should be {partition column name}={value}
If you intends to add the partitions manually then the structure has no meaning.
Any set values can be coupled with any directory. e.g. -
alter table cdrs
add if not exist partition (record_type='TYP123',createdate=date '2017-03-22')
location 'hdfs://nameservice1/tmp/sbx_unleashed.db/2017MAR22_OF_TYPE_123';
Assuming directory structure -
.../sbx_unleashed.db/record_type=.../createyear=.../createmonth=.../createday=.../
e.g.
.../sbx_unleashed.db/record_type=TYP123/createyear=2017/createmonth=03/createday=22/
create external table cdrs
(
record_id int
,record_detail tinyint
,datetime_start int
)
partitioned by (record_type int,createyear int, createmonth tinyint, createday tinyint)
row format delimited
fields terminated by '|'
lines terminated by '\n'
stored as TEXTFILE
location 'hdfs://nameservice1/tmp/sbx_unleashed.db'
tblproperties ("skip.header.line.count"="1", "skip.footer.line.count"="1")
;
Assuming directory structure -
.../sbx_unleashed.db/record_type=.../createdate=.../
e.g.
.../sbx_unleashed.db/record_type=TYP123/createdate=2017-03-22/
create external table cdrs
(
record_id int
,record_detail tinyint
,datetime_start int
)
partitioned by (record_type int,createdate date)
row format delimited
fields terminated by '|'
lines terminated by '\n'
stored as TEXTFILE
location 'hdfs://nameservice1/tmp/sbx_unleashed.db'
tblproperties ("skip.header.line.count"="1", "skip.footer.line.count"="1")
;

How to read a field Oracle containing XML

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.

example of an external table which accepts all records from a file

Please can any one tell me how to accept all records from a file so that no record should go to discard or bad file using external table concept.
Below is the code
CREATE TABLE ext_tab2 (
mprn CHAR(10))
ORGANIZATION EXTERNAL (
TYPE ORACLE_LOADER
DEFAULT DIRECTORY IMPORT
ACCESS PARAMETERS (
RECORDS DELIMITED BY NEWLINE
BADFILE IMPORT:'test.bad'
LOGFILE IMPORT:'test.log'
FIELDS TERMINATED BY ','
(mprn char(10)))
LOCATION ('abc.txt')
)
PARALLEL 5
REJECT LIMIT UNLIMITED;
All the data from file('abc.txt') should get inserted into ext_tab2 table .

SQL Truncation Issue Converting VARCHAR to VARBINARY

I have a fairly simple insert from a csv file into a temp table into a table with an encrypted column.
CREATE TABLE table1
(number varchar(32) NOT NULL
, user_varchar1 varchar(65) NOT NULL
, account varchar(32) NOT NULL)
CREATE TABLE #temp1
(number varchar(32) NOT NULL
, user_varchar1 varchar(65) NOT NULL
, account varchar(32) NOT NULL)
OPEN SYMMETRIC KEY SKey
DECRYPTION BY CERTIFICATE CERTCERT
--Flat File Insert
BULK INSERT #temp1
FROM '\\Server\Data\filename.csv'
WITH (FIELDTERMINATOR = ','
, FIRSTROW =2
, ROWTERMINATOR = '\n'
);
INSERT INTO table1
(number, user_varchar1, account_encrypted)
SELECT user_varchar1, number
, ENCRYPTBYKEY(KEY_GUID('SKey'),(CONVERT(varbinary(MAX), account)))
FROM #temp1
--SELECT * FROM #esa_import_ach
DROP TABLE #temp1
SELECT * FROM table1
CLOSE MASTER KEY
CLOSE SYMMETRIC KEY SKey;
The error I receive is
Msg 8152, Level 16, State 11, Line 40
String or binary data would be truncated.
Now if I allow NULLS into table1, it fills with NULLS, obviously. If I omit the account_encrypted column altogether, the script works.
If I use
INSERT INTO table1 (number, user_varchar1, account)
VALUES ('175395', '87450018RS', ENCRYPTBYKEY(KEY_GUID('SKey'), (CONVERT(varbinary(MAX), account)))
there's no problem.
So, is there something wrong with the way I'm executing the BULK INSERT, is it my declaration of the data types or is it the source file itself.
The source file looks like this (just one row):
emp_id, number, account
175395, 87450018RS,GRDI27562**CRLF**
Thanks and I'm hoping this makes sense.
The problem is that your account column is defined as varchar(32).
ENCRYPTBYKEY returns a result with a max size of 8000. That just won't fit in your column. Either expand the column, or cast the result to a smaller size to fit it inside the column. Right now it just won't fit.