Solved

Bulk Insert update through path value

Posted on 2013-01-28
1
671 Views
Last Modified: 2013-01-28
Dear Experts,
I am using below mentioned query to update text file into database. I need to declare @path in bulk insert statement instead of hard coding. Please help.
 
Bulk Insert Emp_DB.dbo.intermediary  From '\\10.0.0.150\att\txtfile.txt' With (FieldTerminator=',', FirstRow=1, rowterminator ='\n')


Need to do like this

Declare @Path Varchar(100)
Set @Path ='\\10.0.0.150\att\txtfile.txt'


Bulk Insert Emp_DB.dbo.intermediary  From @Path  With (FieldTerminator=',', FirstRow=1, rowterminator ='\n')

Error:
Msg 102, Level 15, State 1, Procedure UpdateTxtFile, Line 14
Incorrect syntax near '@Path'.
Msg 319, Level 15, State 1, Procedure UpdateTxtFile, Line 14
Incorrect syntax near the keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon.
0
Comment
Question by:Mehram
1 Comment
 
LVL 57

Accepted Solution

by:
Raja Jegan R earned 500 total points
ID: 38826331
You cannot pass @Path as a variable to BULK INSERT statement. Instead you need to use Dynamic SQL like this..

Declare @Path Varchar(100)
declare @sql nvarchar(1000)
Set @Path ='\\10.0.0.150\att\txtfile.txt'
SET @sql = 'Bulk Insert Emp_DB.dbo.intermediary  From ''' + @path + '''  With (FieldTerminator='','', FirstRow=1, rowterminator =''\n'')'

exec sp_executesql @sql
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

823 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