I have a query which is working fine:
BULK INSERT ZIPCodes
FROM \'e:\\5-digit Commercial.csv\'
WITH
(
FIRSTROW = 2 ,
FIELDTERMINATOR = \',\',
There is an alternative to dynamic SQL if you have access to the SQLCmd exe.
The SqlCmd utility allows you to pass string replacement variables using the -v argument.
You can use a template variable named "filepath" which will be replaced when you execute the script via the cmdline.
The SQL script would look like:
BULK INSERT ZIPCodes
FROM '$(filepath)'
WITH
(
FIRSTROW = 2 ,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
end
You would then execute the script from a commandline using something like the following:
sqlcmd -b -S SERVER\INSTANCEHERE -E -i "PATH\FILENAMEHERE.Sql" -v FilePath = "e:\5-digit Commercial.csv" -s "|"
The important part of the example is the -v argument:
-v FilePath = "e:\5-digit Commercial.csv"