I have a table with a column of project numbers (local IDs) and several columns of different types of metrics. I need a SQL query where both the selected column and the local ID are dynamic and ultimately fed by parameters. The query works, but NOT with the WHERE clause. I get an com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near ‘)’. error.
what am I getting wrong?
DECLARE @sql nvarchar(max) --declare variable
DECLARE @colName nvarchar(max) --declare variable for parameter
DECLARE @localId nvarchar(max) --declare variable for parameter
set @colName = :colNameParameter --assign value to parameter variable
set @localId = '247' --assign value to parameter variable
set @sql = 'SELECT '+ @colName +' FROM ISG_MOD_ELEC_PROGRAMS.ProjectData.ProgramMetricIndicator WHERE localId= '+@localId+')' --build query string with parameter*/
exec(@sql) --execute sql query