Getting error "Error parsing insert statement for table ROOT.LOAD_SQL" while using ltrim in sql loader - oracle

I trying to load the datas from data file to the database table load_sql using sql loader. I have data like below in the data file.
empid,ename
1,Raja,**Kanchi
2,Poo,**Kanchi
3,Bhasker,**Kanchi
4,Siva,**Kanchi
I have to load to load it in the load_sql table like below format:
1,Raja,Kanchi
2,Poo,Kanchi
3,Bhasker,Kanchi
4,Siva,Kanchi
I have written a control file with help of char manipulation function for inserting records in third column but im getting error:
options(skip = 1,Errors = 100, direct = True)
load data
infile 'D:\SQLLDR\control.ctl'
truncate into table load_sql
when city = 'Kanchi'
fields terminated by ','
optionally enclosed by '"'
(
empid,
ename,
X filler,
city "ltrim(:city,*)"
)
I'm getting the error like
'SQL*Loader-951: Error calling once/load initialization
ORA-02373: Error parsing insert statement for table ROOT.LOAD_SQL.
ORA-00936: missing expression'

You have some syntax errors in your control file. Try this:
options(skip = 1,Errors = 100, direct = True)
load data
infile 'D:\SQLLDR\control.ctl' <-- This doesn't look like a data file name?
truncate into table load_sql
when (city = '**Kanchi')
fields terminated by ','
optionally enclosed by '"'
(
empid,
ename,
city "ltrim(:city, '*')"
)

ltrim(:city,*)
^
|
this is invalid
Should have been
ltrim(:city, '*')
or, possibly,
replace(:city, '*', '')

Related

How to insert a variable value into table?

Suppose i have the following file.csv
DATE Name Email
26-Sep-19 Name1 Name1#email.com
26-Sep-19 Name2 Name2#email.com
26-Sep-19 Name3 Name3#email.com
I am trying to insert values from file.csv into a table
import cx_Oracle
import csv
import os
from datetime import datetime
con = cx_Oracle.connect(uname, pwd, hostname + ': ' + port + '/' + service)
cursor = con.cursor()
with open('file.csv', 'r') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
for lines in csv_reader:
cursor.execute( "INSERT INTO table1 ( DATE,Name,Email) values (:1, :2, :3)",
(lines[0],lines[1],lines[2])
cursor.close()
con.commit()
con.close()
I get this error:
(lines[0],lines[1],lines[2]) cx_Oracle.DatabaseError: ORA-01858: a
non-numeric character was found where a numeric was expected
After some debugging, i was able to nail it down to the issue being with the date, so instead of lines[0], i replaced with hard-coded date, and it worked!
cursor.execute( "INSERT INTO table1 ( DATE,Name,Email) values (:1, :2, :3)",
('26-Sep-19',lines[1],lines[2])
why is it not working with lines[0] variable but with a hardcoded value its working just fine?
The string format of your date must match what Oracle is expecting. You can set the NLS_DATE_FORMAT parameter for your session, or you can simply modify your code to do something like the following instead:
cursor.execute("""
insert into table1 (date, name, email)
values (to_date(:1, 'dd-Mon-YY'), :2, :3)""",
(lines[0], lines[1], lines[2]))
Now you've solved the date format issue, consider using executemany() for performance. Something like:
With file.csv containing:
26-Sep-19,Name1,Name1#email.com
26-Sep-19,Name2,Name2#email.com
26-Sep-19,Name3,Name3#email.com
and the table created like:
create table table1 ("DATE" date, name varchar2(20), email varchar2(20));
Then this file using the date conversion that #anthony mentioned works:
# Set cursor sizes to match the table definition or known max data sizes
# create table table1 ("DATE" date, name varchar2(20), email varchar2(20));
cursor.setinputsizes(None, 20, 20)
# Adjust the batch size to meet your memory and performance requirements
batchSize = 1000
with open('file.csv', 'r') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
i = 0
data = []
for line in csv_reader:
data.append((line[0],line[1],line[2]))
i = i + 1
if (i % batchSize == 0):
print('batch')
cursor.executemany("""insert into table1 ("DATE",name, email) values (to_date(:1, 'DD-Mon-YY'), :2, :3)""", data)
data = []
i = 0
if (i % batchSize != 0):
print('final')
cursor.executemany("""insert into table1 ("DATE",name, email) values (to_date(:1, 'DD-Mon-YY'), :2, :3)""", data)
con.commit()
To load some rows to a DB on the far side of the world took 4 seconds with a similar script (mostly connection time costs) vs 36 seconds with execute()

How to add new line separator into data for oracle

I'm working on displaying data from oracle.
is there a way to make the following data inside the table:
example :
'1.somedata, 2.somedata, 3.somedata, 4.somedata, 5.somedata'
to display like:
example:
'1. somedata
2. somedata
3. somedata
4. somedata
5. somedata'
on the interface?
do i add new line separator directly into the data?
or do i separator them into new line when i query it?
or is there any other simple way?
Thanks.
There are so many ways to do this, here is one if you are selecting from a column:
SELECT REPLACE ('1.somedata, 2.somedata, 3.somedata, 4.somedata, 5.somedata', ',', CHR (13) || CHR (10)) AS split
FROM DUAL;
1.somedata
2.somedata
3.somedata
4.somedata
5.somedata
I personally would use the listagg function and use '' as the delimiter.
SELECT LISTAGG(last_name, ' ')
WITHIN GROUP (ORDER BY hire_date, last_name) "Emp_list",
MIN(hire_date) "Earliest"
FROM employees
WHERE department_id = 30;
Remember that Apex is generating a web page, which means the end result is HTML. Apex, however, will also sometimes escape special HTML characters for you, like < and &. Since you're viewing a table, I assume the source of your data is a query and your "somedata" field is a single column. Try this:
SELECT REPLACE( somedata_column, ',', '<br />' )
FROM mytable
You don't say what version of Apex. In Apex 4.x, the column would need to be set to a Standard Report Column, which would stop Apex from the <br> elements. I forget what the column type is in Apex 5.x.
Check below sample query which converts coma separated list data into rows
SELECT substr( '1.AL,2.AL,3.AL,4.AL,5.AL,6.AL,',
( case when rownum = 1 then 1
else instr( '1.AL,2.AL,3.AL,4.AL,5.AL,6.AL,', ',', 1, rownum - 1 ) + 1
end ),
instr( substr( '1.AL,2.AL,3.AL,4.AL,5.AL,6.AL,',
( case when rownum = 1 then 1
else instr( '1.AL,2.AL,3.AL,4.AL,5.AL,6.AL,', ',', 1, rownum - 1 ) + 1
end )
), ',' ) - 1
) as data
FROM dual
CONNECT BY LEVEL <= length( '1.AL,2.AL,3.AL,4.AL,5.AL,6.AL,' ) - length ( replace('1.AL,2.AL,3.AL,4.AL,5.AL,6.AL,', ',') )
Hope this will help you!

How to use trim function in codeigniter active record

I wrote the below in format of codeigniter
$this->db->select("TRIM(BOTH ',' FROM column_name )");
$this->db->get(table_name)->result_array();
But when I printed the query using $this->db->last_query(), I found a space after comma(,) like this
SELECT TRIM(BOTH ', ' FROM column_name ) FROM table_name. How to remove that space so that my query will run.
You can set in the validation message like :
$this->form_validation->set_rules('input_field', 'Input Field', 'trim|required|valid_email|xss_clean');

How to end the load infile process in oracle

I am trying load the data using in ctl file , But syntax error I am getting ,that is :SQL*Loader-350: Syntax error at line 15.
Expecting "," or ")", found keyword by.
COS "TRUNC(:COS/32)",TERMINATED BY WHITESPACE
following code is my try.
load data
INFILE 'rtd.txt'
INTO TABLE RTD_ATTLAS_TMP
APPEND
FIELDS TERMINATED BY '\|'
(
TRAFFIC_CUST_ID "UPPER(:TRAFFIC_CUST_ID)",
SOURCE_DEV_NAME "UPPER(REGEXP_REPLACE(:SOURCE_DEV_NAME, '\\.\\D+', '', 1, 0))",
DEST_DEV_NAME "UPPER(REGEXP_REPLACE(:DEST_DEV_NAME, '\\.\\D+', '', 1, 0))",
DATE_STAMP boundfiller,
TIME_STAMP "to_date(:DATE_STAMP ||' '|| :TIME_STAMP, 'mm/dd/yyyy hh24:mi:ss')",
MIN_RTD,
AVG_RTD,
MAX_RTD,
COS "TRUNC(:COS/32)",TERMINATED BY WHITESPACE
)
Here is my file to be loaded 'rtd.txt':
NEW FEEDS TEST|ARAETSBUEAR01H03|172.20.227.220|10/08/2014|00:00:00|159|159|159|104
I have changed this in my code , it is working fine,
COS TERMINATED BY WHITESPACE "TRUNC(:COS/32)"

Expected CHAR got NUMBER

DB: Oracle 11g
Query:
SELECT CASE
WHEN rs.OPTION = '3'
THEN
(SELECT COUNT(DISTINCT ex.EXTS) AS TMPCOL0
FROM CRSIDM.SUB_OPTS ex
INNER JOIN CRSIDM.SUB_OPTS_GRP cg
ON cg.GROUP_ID = ex.GRP_ID
)
ELSE
(SELECT COUNT(DISTINCT ex.EXTS) AS TMPCOL0
FROM CRSIDM.SUB_OPTS ex
INNER JOIN CRSIDM.SUB_OPTS_POL cg
ON cg.GROUP_ID = ex.GRP_ID
)
END AS PROPTS
FROM PR_OPTS
I am getting error 'expected CHAR got NUMBER', here EXTS,GROUP_ID & GRP_ID are numeric. Then how there is a chance of expecting CHAR?
Generally when Oracle compares different datatypes such as a NUMBER with a CHARACTER, implicit conversion kicks in and all is well (provided the data can be converted.) For example, if you have a function that expects a CHARACTER value but you pass it a NUMBER, all is well - Oracle simply converts the NUMBER to character.
E.g. a function like this:
create or replace function get_something(p_id VARCHAR2) return number ...
works if you call it with this:
get_dno(10);
or this:
get_dno('10');
and in SQL:
select * from some_table where numeric_column = '10' -- no problem.
A popular place where you see this kind of error is with the return values in CASE statements. For instance, you'll get that error if you have something like this:
SQL> SELECT CASE WHEN 1 = 1 THEN '1' ELSE 2 END
2 FROM dual
3 ;
SELECT CASE WHEN 1 = 1 THEN '1' ELSE 2 END
*
ERROR at line 1:
ORA-00932: inconsistent datatypes: expected CHAR got NUMBER
(The datatype from the first WHEN clause is what it expects in the other WHEN/ELSE clauses that follow.)
But in your case the WHEN and THEN both return counts - the datatypes are consistent. So, I think you have a red-herring in there.
As Alex mentioned above, OPTION is a keyword and if you try and create a table with that as a column name, Oracle disagrees:
SQL> create table dummy
2 (option varchar2(10)
3 );
(option varchar2(10)
*
ERROR at line 2:
ORA-00904: : invalid identifier
This works:
SQL> create table dummy
2 (option_col varchar2(10)
3 );
Table created.
or you could do it with quotes:
SQL> create table dummy
2 ("option" varchar2(10));
Table created.
But now you're in a world of hurt - you need quotes from now on:
SQL> select option from dummy;
select option from dummy
*
ERROR at line 1:
ORA-00936: missing expression
SQL> select d.option from dummy d;
select d.option from dummy d
*
ERROR at line 1:
ORA-01747: invalid user.table.column, table.column, or column specification
With quotes:
SQL> select d."option" from dummy d;
no rows selected
So, if your query is really giving you "expected CHAR, got NUMBER", it looks to me like something is off.
Essentially, it means some of the fields you are using aren't compatible with each other. It's basically a "type mismatch". Just check to see if any types of CHAR are being used with types of NUMBER. Then you can either switch the type of one, or simply use a conversion as part of the query.
The issue is OPTION = '3', the quotation marks indicate that you're looking for a string containing the solitary character 3.
Try this instead:
SELECT CASE
WHEN rs.OPTION = 3
THEN
(SELECT COUNT(DISTINCT ex.EXTS) AS TMPCOL0
FROM CRSIDM.SUB_OPTS ex
INNER JOIN CRSIDM.SUB_OPTS_GRP cg
ON cg.GROUP_ID = ex.GRP_ID)
ELSE
(SELECT COUNT(DISTINCT ex.EXTS) AS TMPCOL0
FROM CRSIDM.SUB_OPTS ex
INNER JOIN CRSIDM.SUB_OPTS_POL cg
ON cg.GROUP_ID = ex.GRP_ID)
END AS PROPTS
FROM PR_OPTS

Resources