Solved

Insert txt data into new table

Posted on 2013-05-22
5
246 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how the fundamental information of how to create a table.

803 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