Solved

Bulk Insert update through path value

Posted on 2013-01-28
1
672 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

829 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