Solved

Import txt file from different server into Microsoft SQL 2008

Posted on 2013-01-07
5
445 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

717 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