SQL import/create columns in SMILE chemical structures - sql

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

Related

Script to Import data into SQL table from flat file (text file) .XYZ file

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)"

SQL Server 2012 Bulk Insert from CSV into temp table

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

Bulk insert with Blank values

I have one table with 48 columns in which I want to import data from csv file. My csv file consist of some blank values.
Whenever I uses bulk insert I am getting error:
1) Bulk load data conversion error (type mismatch or invalid character
for the specified codepage) for row 1, column 1 (column name)
2)The OLE DB provider "BULK" for linked server "(null)" reported an
error. The provider did not give any information about the error.
3)Cannot fetch a row from OLE DB provider "BULK" for linked server
"(null)".
I am using sql server 2008
Below is bulk insert command I am using:-
**
bulk insert DataBaseName.dbo.TableName
from 'C:\FolderName\FileName.csv'
with
(
FIRSTROW = 1,
FIELDTERMINATOR =',',
ROWTERMINATOR ='\n',
KEEPNULLS
)**
Please suggest how to handle it..?
For this type of errors make sure the below things:
1.The datalength should be matched according to your .CSV file(use trial and error method and reach your lengths).
The number of columns should be matched(need to check manually).
The datatype conversions should be done implicitly(better to use all nvarchar datatype in order to avoid errors).

Dataimport into SQL server 2008 r2 EXPRESS from SQL Anywhere 11.0

Hello Everyone Iam trying to bulk insert data from .dat file(unloaded from sybase) in to the table i created.
Here is the Table Definition:
CREATE TABLE dbo."GCK_KioskSetupOptions" (
"SMTPIPAddress" varchar(128) NULL
,"SMTPPort" varchar(20) NULL
,"POP3IPAddress" varchar(128) NULL
,"POP3Port" varchar(20) NULL
,"AutoLogOffDelay" integer NULL
,"AllowUserToPrint" bit NOT NULL
,"AllowAttachment" bit NOT NULL
,"MaxMessageSize" integer NULL
,"BillingCodeOptionID" integer NOT NULL
,"MaxMessage" integer NULL
,"AllowUserToImport" bit NOT NULL
,"ProtectUserWithPassword" varchar(50) NULL
,"DefaultEncoding" varchar(50) NULL
,"EnableMessageLimit" bit NULL
,"GlobalMessageLimit" integer NULL DEFAULT 30
,"AllowSetMessagePriority" bit NOT NULL DEFAULT 0
)
go
There is only a single row ( because of empty database) in .dat file which needs to be inserted into the table mentioned above
Here is .dat file data format
'127.0.0.1','25','127.0.0.1','110',10,1,1,400,2,250,1,
Here is the Bulk Inset command
BULK
INSERT GCK_KioskSetupOptions
FROM 'C:\Program Files\Globe Wireless\WebMail\Database\Defaults\Data\404.dat'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
Running this command in SQLServer Management Studio iam getting these errors
Msg 4832, Level 16, State 1, Line 2
Bulk load: An unexpected end of file was encountered in the data file.
Msg 7399, Level 16, State 1, Line 2
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 2
Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)".
Could anyone help me out solving this issue?.I am actually doing datamigration from Sybase SQL Anywhere 11 to SQL server 2008 R2 express and this is the first issue i am facting when trying to import data from .dat file into SQL Server 2008 r2 EXPRESS table.
Thankyou
By the looks of things there are ll fields in the sample data file but 16 columns in your table.
I think you need to pad out the input with the defaults.
Instead of just
'127.0.0.1','25','127.0.0.1','110',10,1,1,400,2,250,1,
you need
'127.0.0.1','25','127.0.0.1','110',10,1,1,400,2,250,1,,,,,

SQL BULK INSERT FROM errors

I'm attempting to insert a CSV file into an Microsoft SQL Server Management Studio database like this:
BULK INSERT [dbo].[STUDY]
FROM 'C:\Documents and Settings\Adam\My Documents\SQL Server Management Studio\Projects\StudyTable.csv'
WITH
(
MAXERRORS = 0,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
But I am getting errors:
Msg 4863, Level 16, State 1, Line 2
Bulk load data conversion error (truncation) for row 1, column 9 (STATUS).
Msg 7399, Level 16, State 1, Line 2
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 2
Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)".
Unfortunately, I cannot provide the contents of StudyTable.csv, to protect the privacy of the company we're working for.
EDIT I can vouch for the validity of the csv file though. It was exported from OpenOffice's version of Excel, and I went through and made sure it was valid.
EDIT2
Here's a dummy version of the CSV file:
1234,,,1234,1234,,"asdf","asdf","Z","asd",7/1/2010 12:23,8/5/2010 13:36,9/4/2010 13:36,"(asdf,1661,#1234,F,T)","F",,,"F",,"68866",1234,1234,1234,"F"
Here's a create script for the STUDY table:
CREATE TABLE [dbo].[STUDY]
(
[STUDY_ID] INT IDENTITY(1,1) NOT NULL,
[PARENT_ID] INT,
[GROUP_ID] INT,
[WORKFLOW_NODE_ID] INT,
[STUDY_TEMPLATE_ID] INT,
[INSPECTION_PLAN_ID] INT,
[NAME] VARCHAR(255),
[DESCRIPTION] VARCHAR(4000),
[STATUS] VARCHAR,
[OLD_STATUS] VARCHAR,
[CREATED_ON] DATE,
[COMPLETED_ON] DATE,
[AUTHORIZED_ON] DATE,
[EVENTS] VARCHAR,
[NEEDS_REVIEW] CHAR,
[HAS_NOTES] CHAR,
[HAS_AUDITS] CHAR,
[STUDY_PART] CHAR,
[STUDY_TYPE] VARCHAR,
[EXTERNAL_REFERENCE] VARCHAR,
[CREATED_BY] INT,
[COMPLETED_BY] INT,
[AUTHORISED_BY] INT,
[ARCHIVED_CHILD_COMPLETE] CHAR
)
It sounds like the data in your STATUS column in the .csv file is longer than the definition for the field in your SQL Table.
Check the definition on that field and make sure the data you have in the .csv will fit (both length and type).
The first error is likely because a column is too big to fit the database. First thought is does the CSV file contain column headers?
The second error is very generic.
Do any of your fields contain a comma as part of their data - for example 37, High Street which isn't encased in quotes? I've come unstuck lots of times in the past because of "issues" in the data (the data is valid, just not quite what's expected). If your CSV file is a few thousand rows or more - what happens if you try importing just the first hundred (or ten) rows?