473,729 Members | 2,309 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Import flat file into SQL Server 2005 Express

I am new to SQL Server, and migrating part of an Access application to
SSE. I am trying to insert a comma delimited file into SSE 2005. I am
able to run a BULK INSERT statement on a simple file, specifying the
field (,) and row (\n) terminators. I can also do the same with a
format file.

Here is the problem. My csv file has 185 columns, with a mixture of
datatypes. Sometimes, a text field will contain the field delimiter as
part of the string. In this case (and only in this case) there will be
double quotes around the string to indicate that the comma is part of
the field, and not a delimiter.

Is there any way to indicate that there is a text delimiter that is
only present some of the time?

If not, any suggestions on getting the data into SSE?

Many thanks for your input.

Cheryl

Jan 5 '07 #1
9 21835
(ca*******@opto nline.net) writes:
I am new to SQL Server, and migrating part of an Access application to
SSE. I am trying to insert a comma delimited file into SSE 2005. I am
able to run a BULK INSERT statement on a simple file, specifying the
field (,) and row (\n) terminators. I can also do the same with a
format file.

Here is the problem. My csv file has 185 columns, with a mixture of
datatypes. Sometimes, a text field will contain the field delimiter as
part of the string. In this case (and only in this case) there will be
double quotes around the string to indicate that the comma is part of
the field, and not a delimiter.
So a file could look like this:

2,34,Enter Sandman,Pat Boone
9,34,Zabadak,"D ave, Dee, Dozy, Mich & Tich"
8,981,"Rebel, Rebel",David Bowie

There is now way to get BULK INSERT to handle this file in that shape.
If I were faced with this file, I would write Perl script that replaced
the commas outside the "" with a different delimiter and then removed the
"". And it would not be trivial.

Most other people would probably try to write a package in Integration
Services, but I have never used Integration Services myself. And for your
part - SQL Express does not come with Integration Services, I believe.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jan 6 '07 #2
In message <Xn************ **********@127. 0.0.1>, Erland Sommarskog
<es****@sommars kog.sewrites
(ca*******@opto nline.net) writes:
>I am new to SQL Server, and migrating part of an Access application to
SSE. I am trying to insert a comma delimited file into SSE 2005. I am
able to run a BULK INSERT statement on a simple file, specifying the
field (,) and row (\n) terminators. I can also do the same with a
format file.

Here is the problem. My csv file has 185 columns, with a mixture of
datatypes. Sometimes, a text field will contain the field delimiter as
part of the string. In this case (and only in this case) there will be
double quotes around the string to indicate that the comma is part of
the field, and not a delimiter.

So a file could look like this:

2,34,Enter Sandman,Pat Boone
9,34,Zabadak,"D ave, Dee, Dozy, Mich & Tich"
8,981,"Rebel, Rebel",David Bowie

There is now way to get BULK INSERT to handle this file in that shape.
If I were faced with this file, I would write Perl script that replaced
the commas outside the "" with a different delimiter and then removed the
"". And it would not be trivial.

Most other people would probably try to write a package in Integration
Services, but I have never used Integration Services myself. And for your
part - SQL Express does not come with Integration Services, I believe.
Two things to add, both useful options if the amount of data is small.
First, the import filters in MS Access are better than those in SQL
Server. If the data will fit into an Access table that might just do the
trick. Second, spreadsheets have more flexible parsing options than
databases. It may be possible to load the data into a spreadsheet. That
allows different algorithms to be applied to different rows.

Lastly, text files can be opened and read by VBA code in any of the
office languages, or any of the .NET languages. Either could be used,
but writing code to cope with all of the possible options may take time.
--
Bernard Peek
back in search of cognoscenti
Jan 6 '07 #3
Erland Sommarskog (es****@sommars kog.se) writes:
So a file could look like this:

2,34,Enter Sandman,Pat Boone
9,34,Zabadak,"D ave, Dee, Dozy, Mich & Tich"
8,981,"Rebel, Rebel",David Bowie

There is now way to get BULK INSERT to handle this file in that shape.
If I were faced with this file, I would write Perl script that replaced
the commas outside the "" with a different delimiter and then removed the
"". And it would not be trivial.
In addition to Bernard's post, is not Excel able to read that format?
In such case open in Except, and save as a tab-delimited file and importing
that should be a breeze. (Assuming, of course, there are no tabs in the
data!)

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jan 6 '07 #4
Thank you both for your suggestions. Yes, I was thinking that BULK
INSERT was not going to be able to handle this. I had thought about
dumping the file into an Access table first, but the file could be very
large (200,000+ rows). I am going to try the Excel spreadsheet idea.

Erland Sommarskog wrote:
Erland Sommarskog (es****@sommars kog.se) writes:
So a file could look like this:

2,34,Enter Sandman,Pat Boone
9,34,Zabadak,"D ave, Dee, Dozy, Mich & Tich"
8,981,"Rebel, Rebel",David Bowie

There is now way to get BULK INSERT to handle this file in that shape.
If I were faced with this file, I would write Perl script that replaced
the commas outside the "" with a different delimiter and then removed the
"". And it would not be trivial.

In addition to Bernard's post, is not Excel able to read that format?
In such case open in Except, and save as a tab-delimited file and importing
that should be a breeze. (Assuming, of course, there are no tabs in the
data!)

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jan 8 '07 #5
(ca*******@opto nline.net) writes:
Thank you both for your suggestions. Yes, I was thinking that BULK
INSERT was not going to be able to handle this. I had thought about
dumping the file into an Access table first, but the file could be very
large (200,000+ rows). I am going to try the Excel spreadsheet idea.
200000+ rows? Then Access is probably a better bet. Doesn't Excel stop
at 65536 rows?

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jan 8 '07 #6
Erland Sommarskog wrote:
(ca*******@opto nline.net) writes:
>Thank you both for your suggestions. Yes, I was thinking that BULK
INSERT was not going to be able to handle this. I had thought about
dumping the file into an Access table first, but the file could be very
large (200,000+ rows). I am going to try the Excel spreadsheet idea.

200000+ rows? Then Access is probably a better bet. Doesn't Excel stop
at 65536 rows?
I think the latest version of Excel may have a higher row limit - which
only increases the tendency of newbies to misuse Excel as a "database".
Jan 9 '07 #7
You are correct - Excel has a limit on the number of rows. I thought
about that after I sent the reply. So now I am looking at Access.

Here is my next question. I want to use OPENROWSET in a procedure to
get the data from Access into SSE. My code looks something like this:

INSERT INTO sse_table1 Select GFF.*
FROM OPENROWSET(
'Microsoft.Jet. OLEDB.4.0',
'path to mdb';'admin';'' ,
'Select * FROM access_table1
) as GFF

This works great. However, the location of the access database is only
known at runtime. I can pass the path as a parameter to the stored
procedure, but using it as a variable in OPENROWSET fails. Code looks
like this

CREATE PROCEDURE [dbo].[spImportBilling File]
@strTableLocati on varchar(255),
@btSuccess bit OUTPUT
AS
BEGIN

DECLARE @strConnect varchar(255)
SET @strConnect = @strTableLocati on

INSERT INTO tbl_ups_eInvoic e_tmpData Select GFF.*
FROM OPENROWSET(
'Microsoft.Jet. OLEDB.4.0',
' + @strConnect + ';'admin';'',
'Select * FROM tbl_ups_eInvoic e_tmpData'
) as GFF

Set @btSuccess = 1

END

Does OPENROWSET not allow a variable to be used?

Thanks again for the help.

Ed Murphy wrote:
Erland Sommarskog wrote:
(ca*******@opto nline.net) writes:
Thank you both for your suggestions. Yes, I was thinking that BULK
INSERT was not going to be able to handle this. I had thought about
dumping the file into an Access table first, but the file could be very
large (200,000+ rows). I am going to try the Excel spreadsheet idea.
200000+ rows? Then Access is probably a better bet. Doesn't Excel stop
at 65536 rows?

I think the latest version of Excel may have a higher row limit - which
only increases the tendency of newbies to misuse Excel as a "database".
Jan 9 '07 #8
(ca*******@opto nline.net) writes:
INSERT INTO tbl_ups_eInvoic e_tmpData Select GFF.*
FROM OPENROWSET(
'Microsoft.Jet. OLEDB.4.0',
' + @strConnect + ';'admin';'',
'Select * FROM tbl_ups_eInvoic e_tmpData'
) as GFF

Set @btSuccess = 1

END

Does OPENROWSET not allow a variable to be used?
No. Either you have to use dynamic SQL, or define a linked server on the
fly. The former is probably simpler. Look at
http://www.sommarskog.se/dynamic_sql.html#OPENQUERY for a similar example
on how to deal with the nested strings.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jan 9 '07 #9
Thanks for all the info - I figured out how to use the dynamic sql.

Erland Sommarskog wrote:
(ca*******@opto nline.net) writes:
INSERT INTO tbl_ups_eInvoic e_tmpData Select GFF.*
FROM OPENROWSET(
'Microsoft.Jet. OLEDB.4.0',
' + @strConnect + ';'admin';'',
'Select * FROM tbl_ups_eInvoic e_tmpData'
) as GFF

Set @btSuccess = 1

END

Does OPENROWSET not allow a variable to be used?

No. Either you have to use dynamic SQL, or define a linked server on the
fly. The former is probably simpler. Look at
http://www.sommarskog.se/dynamic_sql.html#OPENQUERY for a similar example
on how to deal with the nested strings.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jan 10 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
13310
by: BravesCharm | last post by:
I am trying to connect to SQL Server 2005 Express with Visual C# 2005 Express using this code: static void Main(string args) { SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\temp.MDF;User ID=MyUsername;password=MyPass;DATABASE=Test;Integrated Security=False;User Instance=True"); conn.Open();
1
1210
by: Loane Sharp | last post by:
Hi there I'm not sure that this is strictly relevant to the ASP.NET newsgroup, but perhaps you've encountered a similar problem. Please help!!! I'm using the "System.Data.SqlClient" namespace in an ASP.NET application to connect to a SQL Server 2005 Express database. Moreover, I'm using XCopy and "Database=temp;AttachDBFilename='c:\temp\temp_Data.mdf'" in the connection string when I attach to the database for the first time. (The...
1
6630
by: Peter | last post by:
I've purchased VS.NET 2005 Standard and have tried to install SQL Server 2005 Express, but get the following error in the error log. Please could someone help me.... Microsoft SQL Server 2005 Express Edition x86: Component Microsoft SQL Server 2005 Express Edition x86 returned an unexpected value. ***EndOfSession***? Microsoft SQL Server 2005 Express Edition x86: Component Microsoft SQL Server 2005 Express Edition x86 returned an...
4
5297
by: jmnaste | last post by:
Hi, I tried to migrate data of an Access 2003 application (.mdb) to SQL Server 2005 Express. Using the Wizard and choosing 'create a new database', I receive (a French message saying roughly) '... migration wizard requires SQL Server version 6.5 sp 5 and later ... connect to an SQL server source of data.'. Given that failure, I went to another PC running MSDE (according to
1
1167
by: AJ | last post by:
Hi All, I am reading a tutorial,that seems to suggest that i can deploy SQL Server based websites without having SQL Server installed on the server. The impression i have is that i can connect to the '.MDF' file as a standard file, rather than having to go through the database server. Is this right?
2
2096
by: kress1963nov22 | last post by:
I recently purchased a good MS book ("Build a Web Site Now") by Jim Buyens. It has the Express Edition of MS-Visual Web Developer 2005 on CD and also MS SQL Server 2005 Express Edition on the CD. A couple of questions for this newbie: Can MS SQL Server 2005 Express Edition be used in place of the older MSDE product for backend database work? I also have Visual Studio .NET (VB) 2003 (Standard Edition). How compatible are all these products...
2
6225
by: raylopez99 | last post by:
I am trying to program a database from inside C++.NET via Visual Studio 2005 using the ADO.NET set of classes, but this I believe is a SQL Server 2005 Express permissions question under Windows XP Professional OS. In another thread, http://tinyurl.com/373lzx, I was able to open a database successfully using VS2005 (thanks to a suggestion by Erland Sommarskog). But subsequent to this I played around with the local policy permissons...
1
7422
by: fanoftvb | last post by:
Hi, i'm using SQL server 2005 express and want to import and export my data. However, when i run the C:\Program Files\Microsoft SQL Server\90\DTS\Binn\DTSWizard.exe , i cannot find the DTSWizard.exe. Can someone tell me what to download so that the DTSWizard will be available. Thanks a lot.
1
1419
by: marissaj | last post by:
I have SQL Server 2005 Express Edition installed on my local PC. I can create a new database, create tables,ect. This is done on the Server Management Studio Express. In order to import or export, you have to right click on your database name, Tasks, and you have the import and export options. However, if I right click on my database name, Tasks, I dont have the import or export options. I only have Detach, Scrink, Back Up, Restore and...
1
3830
by: pompair | last post by:
Hi, Could someone give a pointer how to import couple of hundred images into Sql Server 2005 Express Edition database? Is there a tool for it? Can it be done with Sql Management Studio or is it just a matter of writing own piece of software (a little helper app) to do it? -timonardo
0
8917
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8761
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9281
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2163
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.