Bulk Insert not working in SQL Server 2008 - sql-server-2008

Here's my SQL to bulk load a CSV file into SQL Server 2008, but its returning:
0 row(s) affected.
Code:
USE energyDB
GO
BULK INSERT energydata
FROM 'c:\temp\24544_MSSQL_out.csv'
WITH
(
FIRSTROW = 2,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
The CSV file looks like this (the top line is ignored)
24544,"1970-01-01 10:00:00","8056060 kWh"
24544,"2012-12-04 00:15:00",0.176
24544,"2012-12-04 00:30:00",0.163
24544,"2012-12-04 00:45:00",0.016

Bulk insert doesn't remove quotes from the data, you'll either need to change the file being imported or import to a table where every column is a character field and strip the quotes and convert datatypes in a query. I've just found the following MSDN article:
http://msdn.microsoft.com/en-us/library/ms188609.aspx
It states:
Comma-separated value (CSV) files are not supported by SQL Server bulk-import operations.
And then continues on with a few examples of the situations it will work under.

This question is a little old, but it showed up when I searched for the problem, so I thought I would provide my solution.
In my case it was a simple mistake of not inserting into the right table, so be sure to check the table that you are inserting into. In trying to figure out what was going on though, I found that you can have the bulk insert process create an error file that will hopefully guide you in the right direction. To do this, you can use something like ERRORFILE ='E:\Error.txt'. This should output the error you are receiving to a file called Error.txt. I've provided a full example below:
BEGIN TRANSACTION
BEGIN TRY
BULK INSERT [Table Name]
FROM 'E:\FileName.csv'
WITH (FIELDTERMINATOR = ',', ROWTERMINATOR = '\n', ERRORFILE ='E:\Error.txt')
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
END CATCH
You'll notice that I've wrapped the bulk insert process in a Transaction. This is so that if any issue occurs during the bulk insert it will rollback everything and I won't get a partial data import.

Related

Executing sql strings from csv into mysql database

I've created a huge .csv with only one column, each column is a valid sql string update like:
UPDATE TBL SET LAST = 0 WHERE ID = 1534781;
Is there a way to execute each row as a single sql query? Also, I'm using datagrip, if anyone knows of a sort of tool I would be happy.
To execute a file against your database in DataGrip just use the context menu when observing your file in Files tool window
A CSV file that contains one column is just called a "file." :-)
The most common way of executing a series of SQL statements in a file is with the command-line mysql client:
mysql -e "source myfile.csv"
How about script:
begin
update ...
update ...
update ...
...
end;

Output CSV file with trigger in Firebird

Is it possible to create a trigger that outputs a CSV file selecting Fields 1 from Table 1 right after Table 2 is updated?
I have tried using
CREATE OR ALTER trigger test_a0 for Table 2
active after insert or update position 0
AS
begin
if (updating and new.field1 is not null) then
output ('C:\test\test.csv');
select field1 from table1;
output;
commit;
end
No, this it is not possible to output to a CSV file in triggers in Firebird 2.5. If you want to output to a file, you either need to do that in a client application, or use an external table (which technically is a binary format, not a text format). It might be possible to create a convoluted solution using UDFs.
In Firebird 3, a simpler solution might be possible using a UDR (User Defined Routines), but this is largely unknown territory, so I'm not actually sure if it can be done that way.
I suppose you can do it with IBExpert tools ibeblock
execute ibeblock
as
begin
txt='';
for
select firstname, lastname
from customer
into :fn,:ln
do
begin
txt=txt+fn+';'+ln+ibec_crlf();
end;
ibec_SaveToFile('C:\txt.csv',txt,__stfOverwrite);
end

MYSQL Bulk insert failed to import the CSV file

I have a CSV file with field delimiter as '~:-' and row delimiter as '!^('. When I execute the Bulk insert query as follows
QUERY FOR INSERTION:
"BULK INSERT SAMPLETABLE FROM sample.csv WITH (FIELDTERMINATOR = '~:-',ROWTERMINATOR = '!^(',ERRORFILE = 'C:/log/error.log',KEEPIDENTITY, KEEPNULLS,FIRSTROW = 2, DATAFILETYPE='widechar')".
I get the SQLException: Bulk load: An unexpected end of file was encountered in the data file.
I made sure the end of file is not '\n'. The other tables which are working, does not end with '\n'.
Even though ERRORFILE option is enabled in the SQL query, I am not seeing the error.log file getting created when SQLException is thrown.
Finally, found the root cause, I was doing a data migration from older version of the database to newer version. Found out that there is a schema change in the newer table, hence i got SQLException. Irony is that the Exception thrown didn't specifically indicate the Schema change.

Why are these comments in a mysql file blowing up?

On the Linux command line I call this script via the mysql terminal using source...
--First insert new template types
insert into TemplateType(id,name,size)
values (10,'Feature Sheet','8.5x11 ss');
insert into TemplateType(id,name,size)
values (11,'Feature Sheet','8.5x11 ds');
insert into TemplateType(id,name,size)
values (12,'Feature Sheet','11x17');
When I do I get an error saying my sql syntax is invalid. Is this not the correct way to comment an sql file?
Apparently you need whitespace after the -- or use a # instead....
http://dev.mysql.com/doc/refman/5.0/en/comments.html

How do I execute multiple SQL Statements in Access' Query Editor?

I have a text file with a few SQL statements in it that I want to run
on an Access database. I thought that should be possible with Access'
Query Editor. So, I go into this editor and paste the statements:
insert into aFewYears (yr) values ('2000')
insert into aFewYears (yr) values ('2001')
insert into aFewYears (yr) values ('2002')
insert into aFewYears (yr) values ('2003')
Trying to run them (by hitting the red exclamation mark) I receive a
Missing semicolon (;) at end of SQL statement.
This could be taken as an indication that the editor would allow to execute
multiple statements. So, I change the statements and append such a
semicolon at the end:
insert into aFewYears (yr) values ('2000');
insert into aFewYears (yr) values ('2001');
insert into aFewYears (yr) values ('2002');
insert into aFewYears (yr) values ('2003');
Then I get a
Characters found after end of SQL statement.
which probably could be taken as an indication that it is not possible
to execute multiple statements.
Ok, so the question: is it possible to execute multiple statements in the
query editor, or is it possible to somehow batch-execute sql statements in a
file in/on/against Access.
Thanks / Rene
edit The insert statements were used as an example and I realize that they are less than perfect, because they all go to the same table and such a thing can obviously somehow be solved by using one statement that has a union or something. In my actual case that I am trying to solve, the file contains not only insert statements but also create table statements and insert statements with different underlying tables. So I hoped (and still hope) that there is something like my beloved SQL*Plus for Oracle that can execute a file with all kinds of SQL Statements.
You can easily write a bit code that will read in a file. You can either assume one sql statement per line, or assume the ;
So, assuming you have a text file such as:
insert into tblTest (t1) values ('2000');
update tbltest set t1 = '2222'
where id = 5;
insert into tblTest (t1,t2,t3)
values ('2001','2002','2003');
Note the in the above text file we free to have sql statements on more then one line.
the code you can use to read + run the above script is:
Sub SqlScripts()
Dim vSql As Variant
Dim vSqls As Variant
Dim strSql As String
Dim intF As Integer
intF = FreeFile()
Open "c:\sql.txt" For Input As #intF
strSql = input(LOF(intF), #intF)
Close intF
vSql = Split(strSql, ";")
On Error Resume Next
For Each vSqls In vSql
CurrentDb.Execute vSqls
Next
End Sub
You could expand on placing some error msg if the one statement don't work, such as
if err.number <> 0 then
debug.print "sql err" & err.Descripiton & "-->" vSqls
end dif
Regardless, the above split() and string read does alow your sql to be on more then one line...
Unfortunately, AFAIK you cannot run multiple SQL statements under one named query in Access in the traditional sense.
You can make several queries, then string them together with VBA (DoCmd.OpenQuery if memory serves).
You can also string a bunch of things together with UNION if you wish.
Better just create a XLSX file with field names on top row.
Create it manually or using Mockaroo.
Export it to Excel(or CSV) and then import it to Access using New Data Source -> From File
IMHO it's the best and most performant way to do it in Access.
"I hoped (and still hope) that there is something like my beloved SQL*Plus for Oracle that can execute a file with all kinds of SQL Statements."
If you're looking for a simple program that can import a file and execute the SQL statements in it, take a look at DBWConsole (freeware). I have used it to process DDL scripts (table schema) as well as action queries. It does not return data sets so it's not useful for SELECT queries. It supports single line comments prefixed by -- but not multi-line comments wrapped in /* */. It supports command line parameters.
If you want an interactive UI like Oracle SQL Developer or SSMS for Access then Matthew Lock's reference to WinSQL is what you should try.
You might find it better to use a 3rd party program to enter the queries into Access such as WinSQL I think from memory WinSQL supports multiple queries via it's batch feature.
I ultimately found it easier to just write a program in perl to do bulk INSERTS into an Access via ODBC. You could use vbscript or any language that supports ODBC though.
You can then do anything you like and have your own complicated logic to handle the importing.
create a macro like this
Option Compare Database
Sub a()
DoCmd.RunSQL "DELETE * from TABLENAME where CONDITIONS"
DoCmd.RunSQL "DELETE * from TABLENAME where CONDITIONS"
End Sub