BULK INSERT into specific columns? - sql

I want to bulk insert columns of a csv file to specific columns of a destination table.
Description - destination table has more columns than my csv file. So, I want the csv file columns to go to the right target columns using BULK INSERT.
Is this possible ? If yes, then how do I do it ?
I saw the tutorial and code at - http://blog.sqlauthority.com/2008/02/06/sql-server-import-csv-file-into-sql-server-using-bulk-insert-load-comma-delimited-file-into-sql-server/
and http://www.codeproject.com/Articles/439843/Handling-BULK-Data-insert-from-CSV-to-SQL-Server
BULK INSERT dbo.TableForBulkData
FROM 'C:\BulkDataFile.csv'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
They don't show you how you can control where data is inserted.

Yes, you can do this. The easiest way is to just create a View that Selects from the target table, listing the columns that you want the data to go to, in the order that they appear in the source file. Then BULK INSERT to your View instead of directly to the Table.

Related

How to recalculate table created by CTAS?

I have created table using this statement:
CREATE TABLE tablename STORED AS PARQUET AS (SELECT ...)
How can i recalculate it without DROP TABLE - CREATE TABLE flow?
In Impala, The INSERT INTO syntax appends data to a table. The existing data files are left as-is, and the inserted data is put into one or more new data files.
The INSERT OVERWRITE syntax replaces the data in a table. Currently, the overwritten data files are deleted immediately; they do not go through the HDFS trash mechanism.
So If you want to replace the data in the table tablename without undergoing drop table and create table, you can run a query like this
INSERT OVERWRITE TABLE tablename SELECT * from <source_tablename>;

import two columns from a text file into a sql table

I have a table in SQL Server with some columns and a text file. I need to import data of two columns of text file into SQL table (two columns exist in SQL table for do it and no need two insert columns). How can I do it?
Use the SQL Server Importing Wizard and just ignore the columns in the mapping that are not required.
See link.
SQL Server Management Studio (SSMS) provides the Import Wizard task which you can use to copy data from one data source to another. You can choose from a variety of source and destination data source types, select tables to copy or specify your own query to extract data, and save your work as an SSIS package. In this section we will go through the Import Wizard and import data from an Excel spreadsheet into a table in a SQL Server database.
https://www.mssqltips.com/sqlservertutorial/203/simple-way-to-import-data-into-sql-server/
FOR CSV
// THIS IS THE DATA IN THE CSV FILE
Name,Class
Prabhat,4
Prabhat1,5
Prabhat2,6
// end OF CSV FILE
THE QUERY
CREATE TABLE CSVTest (Name varchar(100) , class varchar(10))
BULK
INSERT CSVTest
FROM 'C:\New folder (2)\testcsv.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
--Check the content of the table.
SELECT *
FROM CSVTest
GO
--Drop the table to clean up database.
DROP TABLE CSVTest
GO

Bulk Insert text file with additional column

I am using SQL Server 2008 and I have a table that has two columns. One column is for a row of text and the second is for the name of the text file. I am using the command below. This works fine to insert the rows of text but is there any way to also insert the file name test.txt with all the rows?
BULK INSERT viewFile FROM 'C:\test.txt' WITH (ROWTERMINATOR ='\n')

Bulk insert in SQL and insert others values

I have one table with 3 fields
id_Complex | fileLine | date
The field id_Complex, and, that id_complex is the same for the file, that id just chenge when another file is processed is a ID generate from my program, fileLine is just a line from file and date is the date of recording of the line.
Now, my program make a insert in the database for each line read from the file.
I whant to know, if is possible to make a bulk, and that bulk just insert the values in one specific column of table, and, I just send the id_complex to sql, so, the SQL will be make the insert with id_complex I sent for SQL, the lines of file and date.
How I can make that bulk ?
it's possible to make this, Bulk insert with one that has a value predefined
You should in your program proccess input file and generate temp file with correct complex_id and the make bulk insert for this temp file.
After insert just delete temp file.
If I understand what you are asking, you could create a temporary table TempTable and do a bulk insert into it. Then perform an UPDATE from TempTable joining to your permanent table by id_Complex. You can also set the date in this UPDATE statement. Finally, clear out the temporary table.
Alternatively, you could bulk import the file into a temporary table, delete the old permanent table, and rename the temporary table as the permanent table.

Bulk insert into SQL Server 2005 issue

I've tried bulk insert but I can't get it right. I have a table schema that starts with an id and a few more columns then I need to skip a column. Whats the proper way to assign the columns to each one from a csv?
Thanks
EDIT:
MY Code:
BULK INSERT datadb
from 'C:\datainsert.csv'
WITH
(
FIRSTROW=2,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
The errors is talking about my first column being a different datatype since I have an ID column
I have tested the same and it is working without any issues.
Please post the metadata of your table and csv file, so that I will get a chance to find the issue.
Marc_s nailed it. I created a staging table and bulk loaded to that one. Then did a select into from it into my existing table. Thanks Marc_s!