Solved

Import txt file from different server into Microsoft SQL 2008

Posted on 2013-01-07
5
441 Views
Last Modified: 2013-01-08
Everyday I want to automatically import a txt file (w/yesterday's date) into the sql table LogErrors.  An eample file name is 01062013.txt

So far I have this but I get the error....
      Incorrect syntax near '\'.
How can I fix this?

BULK
INSERT LogErrors
 FROM '\\MED-WKS-21\loadlogs\Txt\' + replace(convert(varchar, DATEADD(d, -1,getdate()), 101), '/', '') + '.txt'

 WITH
 (
 FIELDTERMINATOR = '-',
 ROWTERMINATOR = '\n'
 )
 GO
0
Comment
Question by:BobRosas
  • 2
  • 2
5 Comments
 
LVL 22

Accepted Solution

by:
Steve Wales earned 500 total points
ID: 38753086
It appears, from my testing and some posts I found that you can't put a variable file name in the FROM parameter of BULK INSERT.

A work around was offered here: http://stackoverflow.com/questions/5547393/bulk-insert-with-variable-file-name

I tried a little bit of code myself and was able to make this work:

declare @sql varchar(max);
declare @x char(50);
set @x = 'c:\' + 'x.txt';

set @sql = 'BULK INSERT tab1 from '''+rtrim(@x)+''' WITH (FIELDTERMINATOR = ''-'', ROWTERMINATOR = ''\n'')'
print @sql
exec (@sql)

Open in new window


You could probably build the @sql statement with your variables, I was just doing some quick and dirty testing.  I got failures because there was no x.txt file in c:\ but the above snippet at least got past the errors being generated from the FROM line.
0
 

Author Comment

by:BobRosas
ID: 38753159
Thank you so much for your help!  That helped a lot!  Now I have an access error.  I tried this code...

declare @sql varchar(max);
declare @x char(50);
--set @x = 'c:\' + 'x.txt';
set @x = '\\MED-WKS-21\Txt\' + replace(convert(varchar, DATEADD(d, -1,getdate()), 101), '/', '') + '.txt';

set @sql = 'BULK INSERT LogErrors from '''+rtrim(@x)+''' WITH (FIELDTERMINATOR = ''-'', ROWTERMINATOR = ''\n'')'
print @sql
exec (@sql)

The error I'm getting is this...
     Cannot bulk load because the file "\\MED-WKS-21\Txt\01062013.txt" could not be
     opened. Operating system error code 5(Access is denied.).

It makes sense that it's just a rights issue but I can browse to this location and open the file from the SQL server that I'm on.  Do you now if there is something else I need in the code to allow myself access to the file?  Thanks again!
0
 
LVL 22

Assisted Solution

by:Steve Wales
Steve Wales earned 500 total points
ID: 38753174
Does the user that the SQL Server service runs under have access to that path ?

Check the Security Sections of these two documents:

http://msdn.microsoft.com/en-us/library/ms188365.aspx
http://msdn.microsoft.com/en-us/library/ms175915.aspx
0
 
LVL 4

Expert Comment

by:krtyknmsql
ID: 38753855
Are you running the query using Windows Authentication or Mixed mode? If you use windows Authentication, I hope it will work
0
 

Author Comment

by:BobRosas
ID: 38755731
I ran the same code as admin and it worked fine.  So it was a user access problem.  Thanks again for all help!
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

895 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