I am trying to create a script to import flat files into SQL Server tables. I tried using the import wizard but since I need to do this periodically I would have to create a SQL function in order to achieve this and I am not sure how to go about it. The flat files are stored in the following format:
19350.000 45978.000 1560.631
19352.000 45978.000 1560.234
19354.000 45978.000 1560.021
19356.000 45978.000 1559.809
19358.000 45978.000 1559.596
I have tried the following:
CREATE TABLE #TempTable
(
Id int identity (1,1),
X float,
Y float,
Z float
)
BULK INSERT #TempTable FROM
'\\fcgwnt01\share.$\StandardHaulage\TEST\Automated\EVO\SurfaceFiles\EVO 2019-
01-23.xyz'
WITH (FIELDTERMINATOR = '**\t**', ROWTERMINATOR = '\n')
SELECT * INTO [dbo].[SHM_EVO_SURFACE_DETAILS] FROM #TempTable
--Drop temporary table
DROP TABLE #TempTable
But I'm getting the following errors
Msg 4866, Level 16, State 1, Line 12
The bulk load failed. The column is too long in the data file for row 1, column 1. Verify that the field terminator and row terminator are specified correctly.
Msg 7399, Level 16, State 1, Line 12
The OLE DB provider "BULK" for linked server "(null)" reported an error. The provider did not give any information about the error.
Msg 7330, Level 16, State 2, Line 12
Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)"
As the title says, I am attempting to insert a CSV into a temporary table. I am unfortunately encountering errors.
Here is the query:
USE DATABASE5000
CREATE TABLE #tempTable1
(
ID INT,
CD VARCHAR(50),
ESD DATETIME,
EED DATETIME,
MiscDate DATETIME,
SQ SMALLINT
)
BULK INSERT #tempTable1
FROM 'C:\Dir\Folder\BestFile.csv';
And here are the errors I get:
Msg 4832, Level 16, State 1, Line 1
Bulk load: An unexpected end of file was encountered in the data file.
Msg 7399, Level 16, State 1, Line 1
The OLE DB provider "BULK" for linked server "(null)" reported an error. The provider did not give any information about the error.
Msg 7330, Level 16, State 2, Line 1
Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)".
Any ideas? Thanks yall.
You didn't specified any FIELDTERMINATOR. The default value is actually tab. Please refer to BULK INSERT documentation.
BULK INSERT #tempTable1
FROM 'C:\Dir\Folder\BestFile.csv'
WITH
(
FIELDTERMINATOR = ',' -- add this
);
According to documentation, there is a FORMAT = CSV
WITH (FORMAT = 'CSV')
You may try that. I did a quick test, there are some limitations it seems like does not support string with double quote in it
We try to import a csv file where the first column includes chemical structures (SMILE) like this
c1cccc(c12)n(C)c(c2)CN(C)C(=O)c(c3)ccc(c34)NCC(=O)N(C4)C,14-BENZODIAZEPINEDERIV.4_145,1
c1cccc(c12)n(C)c(c2)CN(C)C(=O)c(c3)ccc(c34)N[C#H](C(=O)N(C4)C)CC(=O)OC,14-BENZODIAZEPINEDERIV.3_146,1
Here is the code in SQL
--Define Table
CREATE TABLE Amide_actives_test
(Structure VARCHAR(40),
Name VARCHAR(40),
Active INT)
GO
--Import Data from CSV
BULK
INSERT Amide_actives_test
FROM 'C:\Amide_actives.csv'
WITH
(
FIELDTERMINATOR = ',', --CSV field delimiter
ROWTERMINATOR = '\n' --Use to shift the control to next row
)
GO
--Check the content of the table
SELECT * FROM Amide_actives_test
GO
The following error message will pop out:
Bulk load data conversion error (truncation) for row 1, column 1 (Name).
Msg 4863, Level 16, State 1, Line 10
...repeating the previous 2 lines 10 times....
The OLE DB provider "BULK" for linked server "(null)" reported an error. The provider did not give any information about the error.
Msg 7330, Level 16, State 2, Line 10
Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)".
Apparently there is a problem of SQL to read the first column in "Structure VARCHAR(40)". I have tried all the string types (CHAR,VARCHAR.NCHAR,NVARCHAR,NTEXT,TEXT) and none of them works.
http://msdn.microsoft.com/en-us/library/ff848814.aspx
There is one way to solve this issue is to purchase another customized MySQL module from DayLight. However, 1. it costs 2. it doesn't support SQL
http://www.daylight.com/dayhtml/doc/pgsql/daycart_pg_search.html
May I know if any SQL guru has SQL solutions? Thanks!
First problem is Structure VARCHAR(40) varchar length is lesser than the input so you got trucation error. Try increasing the varchar length and check
I'm trying to follow the example on this page:
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/
The database is on another machine and I'm connecting to it using SQL Server Management Studio 2012.
Here's the insert statement
BULK
INSERT CSVTest
FROM '\\BPWKS\network share\csvtest.csv'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
When I go to check the content of the table, I get the following error:
Error: Msg 208, Level 16, State 82, Line 1
Invalid object name 'CSVTest'.
Table Definition
Create Table CSVTest
(
ID INT,
FirstName VARCHAR(40),
LastName VARCHAR(40),
BirthDate SMALLDATETIME
)
GO
I've also tried importing the local file at 'C:\csvtest.csv'
Resolution appears to be
Missing Table initially
Missing file or permissions to access
file.
I have a CSV file that contains stock quotes. I am new when it comes to SQL, but I have done a lot of research and come up with a code that I thought should work. But it doesn't. I get errors all the way....
USE ShakeOut
GO
CREATE TABLE CSVTest1
(Ticker varchar(10),
dateval smalldatetime),
timevale time(),
Openval varchar(10),
Highval varchar(10),
Lowval varchar(10),
Closeval varchar(10),
Volume varchar(10),
)
GO
BULK
INSERT CSVTest1
FROM 'c:\TEST.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
--Check the content of the table.
SELECT *
FROM CSVTest1
GO
--Drop the table to clean up database.
DROP TABLE CSVTest1
GO
My CSV file has timevalue as 03:15:00 PM, and I'm not sure how to set that up in the table. The other values I think are aproxmately right, here's a sample of my csv file:
5/1/2009,9:30:00 AM,18.21,18.45,18.21,18.32,32163
5/1/2009,9:35:00 AM,18.33,18.34,18.27,18.29,36951
5/1/2009,9:40:00 AM,18.29,18.38,18.25,18.37,53198
5/1/2009,9:45:00 AM,18.38,18.4,18.28,18.285,49491
And here is my error messages in SQL Management Studio:
Msg 102, Level 15, State 1, Line 4 Incorrect syntax near ','. Msg 208,
Level 16, State 82, Line 3 Invalid object name 'CSVTest1'. Msg 208,
Level 16, State 1, Line 3 Invalid object name 'CSVTest1'. Msg 3701,
Level 11, State 5, Line 3 Cannot drop the table 'CSVTest1', because it
does not exist or you do not have permission.
I would really appreciate help here, my head is about to explode after all these hours without any progression. I've tried MySQL too, didn't work there either.
As I'm new, I might need it explained to the details.
It appears you have an extraneous comma in the CREATE TABLE statement. There is a comma following the final column prior to the closing paren. Perhaps it is valid in some implementations, but you might try removing it. Change it to:
Volume varchar(10)
Ah - and it appears there is an extraneous closing parent in the second column definition. Change it to:
dateval smalldatetime,
And the time column:
timevale time,
Ultimately, it appears you should probably just try to get the CREATE TABLE statement syntax correct, then start adding the other parts.
There is no need for a comma after the last column definition: Volume varchar(10),.
I assume timevale should be timeval.
time() should just be time.
Also, I'm probably being picky but you have capitalised the first letter of all the column names except the first two - won't cause an error but probably better to have a consistent naming convention. I would capitalise the 'v' in val and write the whole word too.
The CSV data needs revising too - you need to specify EVERY column, even if it is null. See my example data (the new lines at the end of each row are for illustration purposes only).
1234567890,2012-08-25,22:15,anytext,ornum,for,varchar,columns <-new line
abcd123456,2010-05-20,00:01,anything,in,these,varchar,columns <-new line
abcd123456,2010-05-20,00:01,anything,in,,,columns <-new line
This works:
CREATE TABLE CSVTest1 (
Ticker varchar(10) NULL,
DateValue smalldatetime NULL,
TimeValue time NULL,
OpenValue varchar(10) NULL,
HighValue varchar(10) NULL,
LowValue varchar(10) NULL,
CloseValue varchar(10) NULL,
Volume varchar(10) NULL)
GO
BULK INSERT CSVTest1
FROM 'C:\TEST.txt'
WITH (FIELDTERMINATOR = ',', ROWTERMINATOR = '\n')
GO
Your CSV file needs to have a new line for each record you want to insert, as specified by the ROWTERMINATOR = '\n' and a comma between each field as specified by FIELDTERMINATOR = ','.
EDIT:
By the way if you are using SQL Server Management Studio (SSMS) you can create the table through the user interface and then:
Right click on the table
Script Table as
CREATE To
New Query Editor Window