clear data and then load data - sql-loader

I'm ruuning below script to insert data from .csv file to oracle table A. How I can update code to clear data of table A first and then insert data using one file.
load data
append
INTO TABLE USB.BAS2_AGENCY_TO_RISKRATING_TRAN
fields terminated by ','
optionally enclosed by '"'
(AS_OF_DATE "TO_DATE(:AS_OF_DATE,'DD-MON-YYYY')"
,MOODY,S_AND_P,FITCH,DBRS,RISK_RATING,BAS_RISK_RATING)

Instead of "APPEND" use "TRUNCATE". Back up first before testing!

Related

How to insert selected columns from a CSV file into one MySQL field using LOAD DATA INFILE [duplicate]

I have a file with some MySQL commands for loading a single column into a database table:
TRUNCATE TABLE datamap;
LOAD DATA LOCAL INFILE 'datamap.txt'
INTO TABLE datamap
Fields terminated by ','
enclosed by '"'
escaped by '^'
Lines terminated by '\r\n'
(datapath);
The datapath file contains a list of files and their directories, i.e. x:\files\filename1.txt
All files are now in a single directory and I would like to hard-code the path into the command above, such that I can load only the filename but have the full path saved to the column in my table. I am aware that I could add an additional UPDATE statement above to update every column in the table after I've loaded it but that doesn't seem very efficient.
Does the LOAD DATA INFILE command support concatenating fixed strings with data being read in from a file?
TRUNCATE TABLE datamap;
LOAD DATA LOCAL INFILE 'datamap.txt'
INTO TABLE datamap
Fields terminated by ','
enclosed by '"'
escaped by '^'
Lines terminated by '\r\n'
(col1, col2, #my_variable)
SET col3 = CONCAT('x:\files\', #my_variable);
http://dev.mysql.com/doc/refman/5.6/en/load-data.html
LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name'
...
[(col_name_or_user_var,...)]
[ SET col_name = expr,...]
When processing an input line, LOAD DATA splits it into fields and uses the values according to the column/variable list and the SET clause, if they are present. Then the resulting row is inserted into the table.

load csv file in mysql table using LOAD DATA LOCAL INFILE command

I want to load csv file into mysql table.The query works fine but in csv row is like :
1000002,Kabul,"Kabul,Afghanistan",2004,AF,City,Active
Kabul and Afghanistan goes into 2 separate columns.below is my query:
LOAD DATA LOCAL INFILE "'.$file.'"
INTO TABLE '.$table.'
FIELDS TERMINATED by \',\'
LINES TERMINATED BY \'\n\'
I want "Kabul,Afghanistan" in one column.
The problem is that you need to add
OPTIONALLY ENCLOSED BY '"'
so that the loader knows to treat the quoted string as a single field.

Retrieved data from one column of database table into Excel

I want to Retrieved data from one of the column of my table name "hote_line", the column name is "elite" and it contains data and I want to retrieve data only from the column "elite". It contains 15,346 names with some info. Is there any way to get that into my Excel so I can make some changes? Because I have to add some id number into each name and it will take long time to edit each name one by one.
First you need to change the engine of the table to allow CSV export:
ALTER TABLE hote_line ENGINE=CSV;
Then,run this query which will export your column into a file called test.csv
SELECT elite
INTO OUTFILE 'test.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n'
FROM hote_line
Excel can than import easily csv files.
Don`t forget to change the engine back to whatever it was,INNODB probably.
EDIT Try it like this:
SELECT elite
INTO OUTFILE 'test.csv'
FROM hote_line

How to format a CSV file for import into multiple MySQL tables

I've been asked to create an excel file saved as a .csv file to import into a mysql db. How do you format the excel file if the data goes to multiple tables in the db?
You need to create separate csv file for each table & then you can use command something like below:
LOAD DATA LOCAL INFILE './csv_data/employees.csv'
INTO TABLE employees
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(first_name,last_name,email_id );
This sample code is under presumption that csv file is comma separated , escape charater is double quote & first line contain header.
You need a CSV file for each table, or you need to import all of your data into an intermediate table and then split that out somehow using either a series of queries, a stored procedure, or a TRIGGER on the import table.
This is because the LOAD DATA INFILE system is limited to one table at a time.

mysql load data local infile

I'm trying to load data into a mysql table using LOAD DATA LOCAL INFILE using the code below.
Mysql:
LOAD DATA INFILE '/var/www/vhosts/domain.com/httpdocs/test1.csv' INTO TABLE temp_table FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES (recloc,client_acc)
Edit: changed LOAD DATA LOCAL INFILE to LOADA DATA INFILE, removed SET id=null, added IGNORE 1 LINES
I'm getting no errors and no imported records. I believe the issue is related to the column names but i'm having a hard time fully understanding what those names should be. Should they be the actual column names within the CSV? or the field names in the DB Table? I would also like the have an auto_incremented primary key (id).
CSV:
recloc,client_acc
"NLGSX3","CORPORATE"
"7SC3BA","QUALITY ASSURANCE"
"3B9OHF","90717-6710"
Any suggestions to what I may be doing wrong? thanks!
Column names in CSV are not necessary, so you should add IGNORE 1 LINES clause.
Columns in your query (recloc,client_acc) need to match columns in table.
First column from CSV will be inserted into recloc, second into client_acc.
If you don't specifu AUTO_INCREMENT column in the statement, but there is one in the table, it should fill automatically.
Short and sweet solution for excel to mysql data import:
Working good for txt file formats.
IN DETAIL:
tbl name=t1
feilds are= name varchar,email varchar;
text.txt file <<== this text file first lines table column names:
name, email
"n1", "e1" next line
"n2", "e2" next line
"n3", "e3" next line
"n4", "e4" next line
"n5", "e5" next line
"n6", "e6" next line
"n7", "e7" next line
pls ignore next line statements
SQL query in wamp
LOAD DATA INFILE 'c:/wamp/www/touch/text.txt' INTO TABLE t1 FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES(name,email)
For this commnad run successfully we have create folders for separately.
Real one is
C:\wamp\mysql\data\wamp\www\touch\text.txt <<==pysical file path is.
But we mention c:/wamp/touch/text.txt