Concatenate Column Names using Dynamic SQL
Posted on 2004-11-16
I need to use Dynamic SQL. For the sake of simplicity, I will omit a large portion of the code along with the parameter passing. I have included the part of the code where I receive an error. Part of the result set is two columns concatenated, separtated by a hyphen.
SELECT BranchNumber + ' - ' + BranchName from MyTable
....This returns: 5050 - Boston
But I get a syntax error on this portion when using dynamic SQL.
exec ('SELECT BranchNumber' + ' - ' + 'BranchName from MyTable')
**BranchNumber and BranchName are both varchar