Solved

Import txt file from different server into Microsoft SQL 2008

Posted on 2013-01-07
5
440 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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

8 Experts available now in Live!

Get 1:1 Help Now