Solved

Insert txt data into new table

Posted on 2013-05-22
5
245 Views
Last Modified: 2016-02-11
Hello everyone,

I have a rather large .txt (50mb) that I need to insert into a table in SQL Server... There are at least 300 columns in this .txt, so I really didn't want to create this table manully, but to use something like
insert * into myTable from *****

Open in new window


I tried this code:
select * from openrowset('MSDASQL', 'Driver={Microsoft Text Driver (*.txt; *.csv)};DefaultDir=M:\Area\POSICON\;','select * from p2m0001_posicon_vcat003cto_GeradaProd_21052013_235311_Copy_21052013_235318.txt');

Open in new window


But it returned my this error:
OLE DB provider "MSDASQL" for linked server "(null)" returned message "[Microsoft][ODBC Text Driver] '(unknown)' is not a valid path.  Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.".
Msg 7303, Level 16, State 1, Line 6
Cannot initialize the data source object of OLE DB provider "MSDASQL" for linked server "(null)".

Open in new window


Any ideas??
0
Comment
Question by:toddinho
  • 3
  • 2
5 Comments
 
LVL 23

Expert Comment

by:nemws1
ID: 39189172
Have you used the Import tool in SSMS?

1) Right Click the database in the Object Explorer in SSMS
2) Select "Tasks"
3) Select "Import Data..."
4) Click Next
5) Under "Data Source" select "Flat File Source"
6) Browse to your file  (by "File Name")
7) Play with the setting and get them to however your TXT file is formatting
8) Click "Next" to see a preview (your field delimiter is set here as well)
9) Click "Next" through the rest of the screens - I tend to not need to touch these
10) Magic occurs (actually SSMS creates your table and loads the text file into it)
11) Profit! :)
0
 

Author Comment

by:toddinho
ID: 39189221
The problem is that it needs to be automatic, via Stored Procedure... I managed to open it (though it came wrongly, because it needs to be treated first), pasting it into local machine directory... When I use some mapped network drive, it cant find the file...

One other thing: is it possible to treat the file before selecting? For example, this .txt has a header with current date, and then just in second line has the columns... So when I select directly I get columns F1,F2,... and the data comes all mixed up...
0
 
LVL 23

Expert Comment

by:nemws1
ID: 39189289
You'll have to create a project in SSIS if you need to import a bunch of these (and skip the first line/etc.)
0
 

Author Comment

by:toddinho
ID: 39199447
Is SSIS compatible with SQL Server 2008 R2 Express??
0
 
LVL 23

Accepted Solution

by:
nemws1 earned 500 total points
ID: 39199531
I'm not 100% positive, but I'm quite sure it does work with the express versions.
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now