Link to home
Start Free TrialLog in
Avatar of toddinho
toddinho

asked on

Insert txt data into new table

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??
Avatar of Nem Schlecht
Nem Schlecht
Flag of United States of America image

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! :)
Avatar of toddinho
toddinho

ASKER

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...
You'll have to create a project in SSIS if you need to import a bunch of these (and skip the first line/etc.)
Is SSIS compatible with SQL Server 2008 R2 Express??
ASKER CERTIFIED SOLUTION
Avatar of Nem Schlecht
Nem Schlecht
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial