Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Import txt file from different server into Microsoft SQL 2008

Posted on 2013-01-07
5
Medium Priority
?
449 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 23

Accepted Solution

by:
Steve Wales earned 2000 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 23

Assisted Solution

by:Steve Wales
Steve Wales earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

597 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