Import CSV file in SQL server - sql

I want to import csv data in sql server. I searched about and found answers about BULK INSERT ... FROM.
The problem I have is :
I want to select just one column of my results
The table already exists with bad datas and I just want to update these fields
The CSV I had contains towns and its parameters (correct datas)
Town,Id,ZipCode,...
T1,1,12000
T2,2,12100
T3,3,12200
And the table in SQL Server 'town' contains for example
T1,1,30456
T2,2,36327
T3,3,85621
I just want to get ZipCode in CSV and update the ZipCode in the table in function the ID.
Does it exist an easy way to do it ?

I normally prefer bcp over bulk insert because with that you can easily import the files over network and there's less issues with access rights. Otherwise I would just load the data into tempdb and update the original table from there.

Related

Sql script to search value in column of database by taking value from a file

I have a csv file with two columns. The file has over 200.000 rows. Inside database I have the same table with the same values.
How can I write a script so that I can search for the values that are present in file but not in database?
I am using SQL Developer for this
Creating an External table is the best option when you want to read the contents of a flat-file using a select query.
Click here to know more about how to create an external table.
After creating the external table, you can make use of a query similar to below to identify the records which are exclusively available in the external table(i.e. flat file).
select *
from new_external_table et
where not exists (select 1 from source_table st where et.column_name=st.column_name);

Oracle SQL: How to Query A CSV With No Header/Column Names?

I have a third party tool which uses CSV Text Drivers which allows for executing SQL queries on CSV data imported into the tool. Most Oracle SQL queries work on this while many don't.
I have a requirement where I have to read and import data into the tool using a CSV file which has no column names or header fields available. How can I execute SQL queries on a table which has no column names or headers defined?
Sample Table:
AB 100 GPAA 9876
AC 101 GPAB 9877
AD 102 GPAC 9878
You would likely need to add the headers before running the queries. Is there a table in which the data will eventually end up? If so, you could export the column names from there first, then append the CSV info to the newly created file afterward.
So apparently, you can specify if your CSV file has a header or not when using the CSV SQL Text driver for interaction with CSV files.
jdbc:csv:////<path_to_file>/?_CSV_Header=false;
Then, we can have a query like
select distinct (column1) as accountID, (column2) as groupID from csv_file_name
The parameters (column1), (column2)... represent the actual columns in the file from left to right and they have to be written like this for the query to work.

Finding rows in table using input from a CSV file - SQL

Using a SQL tool like SQL Developer / Toad for Oracle
Is it possible to write a SQL query that will do the following
SELECT * FROM TABLE
WHERE COLUMN1 IN CSV_FILE
The CSV file is just one column of data with no delimiters.
How can I achieve this?
Constraints
I cannot create a temp table to insert CSV file (no create permissions)
The data I am using of this column is the only index in that table so I cannot use other columns to query or else it will be really slow.
Thanks
Creating external table is the best way. If you dont have permission then the other way is to move the file to the path of any oracle directory(Oracle object - Directory). And with help of utl_file read the file, loop through it and do your operation inside a PL/SQL block which is too tedious.
See the eaxmples for using utl_file - http://psoug.org/reference/utl_file.html
But its better if you try and get create access.
Toad for Oracle data import (uses sqlldr internally)
Create a temp table and load the data using this utility and select the values
External tables
Create external table, load the data through the same and select the values.
Using SQL developer you can create a table in your schema and load this table with data from a csv file.
Notes:
You will need to create a void column per each column to import from excel
Excel export csv with ";" delimiter
If SQL developer(4.1.5) doesn't preview the fields in separated columns try moving forward/backwards with Next/back buttons
and a very graphical guide in the following page:
http://www.thatjeffsmith.com/archive/2012/04/how-to-import-from-excel-to-oracle-with-sql-developer/

Oracle 10g- exporting and importing Table

I need a command for importing a table for the following scenario.
I have a table EMPLOYEE in server A. I am exporting the Table.
I have another table PDATA(having same structure of EMPLOYEE table) in server B.
I need to import the records from EMPLOYEE table(server A) into PDATA table(server B).
I am using Oracle 10g. Please advise.
There are a couple of options. I am going to assume that you don't have any binary data and that the tables aren't absurdly large. We also don't know what type of access you have to either server.
You could use a tool, such as TOAD, to either export to csv or create insert statements. Then execute those on the second server.
You could use PL/SQL and the UTL_FILE library to dump the contents of the table to a csv file. Then mount the csv file as an external table and select into your new table.
If you have the appropriate permissions and the machines can physically see each other you can setup a database link: http://docs.oracle.com/cd/B14117_01/server.101/b10759/statements_5005.htm Once the link is created, you can select from one table into the other.
If you are a DBA then you can use the Export utility, which will export the table into a binary format that can be imported elsewhere.

Which one is good way to Import Excel to Database?

Hi i am using SQL Server 2008.
How can I import an Excel file into the database, which is the easiest way and simple to do?
OpenRowSet
BulkCopy
Linked Servers
SSIS
I have the above options to Import Excel to Database.
In my opinion SSIS wizard is best way to import excel data where you get row and column wise whole view of table data which will be inserted and also specify column names and contraints and parse data using query.
UPDATE :
If the data in your excel file does not require any processing to match your database table then I recommend you save your excel file as a csv and use a combination of BULK INSERT and the BCP.exe program.
To use BULK INSERT you will need a format file which defines how your datafile matches up to your database table. You can write this by hand to match the existing database table or you can use the following command to generate the format file you need:
bcp [ServerName].[SchemaName].[TableName] format nul -c -f [FormatFileOutputName].fmt -S[ServerHostName] -U[DbUserName] -P[DbUserPassword]
Now you will have 2 files:
DatafileName.csv
FormatFileName.fmt.
Use BULK INSERT within Sql Server to insert your data.
Note: If the columns in your datafile are in a different order than your database table then you can simply edit the generated format file to have them map correctly.