Question

I am trying to execute BCP within SSIS to export the results of a query into a CSV file for several different tables. However, for some reason I keep getting parsing errors when I try to put the following into the arguments for the bcp exec:

"SELECT * from myDB.dbo.@[User::Table] " queryout C:\users\MSSQLSERVER\Downloads\@[User::Table].csv -c -t, -T

When I execute BCP with these arguments (changing the variable to an existing table name), everything works fine. I tried to remove several parts of the argument and I still get the following error

Attempt to parse the expression ""SELECT * from myDB.dbo.@[User::Table] " queryout C:\users\MSSQLSERVER\Downloads\@[User::Table].csv -c -t, -T" failed. The expression might contain an invalid token, an incomplete token, or an invalid element. It might not be well-formed, or might be missing part of a required element such as a parenthesis.

What is the issue with my argument?

Was it helpful?

Solution

Your expression is incorrect.

"SELECT * from myDB.dbo.@[User::Table] " queryout C:\users\MSSQLSERVER\Downloads\@[User::Table].csv -c -t, -T

The SSIS expression language isn't like PowerShell where it will see the variable inside the string. Instead, we are back in the stone ages with string concatenation. You also get to deal with escaping slashes and double quotes so your final expression would look something like

"""SELECT * from myDB.dbo." 
+ @[User::Table] 
+ """ queryout C:\\users\\MSSQLSERVER\\Downloads\\" 
+ @[User::Table] 
+ ".csv -c -t, -T"

I find I have the most success when I build complex strings in Variables and then only ever reference the built Variable in an Expression on a Task versus trying to build it in there. That way I can put a breakpoint in a package or raise an Information event back with my Variable's value. Makes debugging a fiddly process a much less painful experience.

bcp reference

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top