I need to do something like this, but it always fails with \'Error converting data type varchar to int\':
DECLARE @intParam INT
DECLARE @ColName VARCHAR(64)
You need to use dynamic sql:
DECLARE @intParam INT
DECLARE @ColName VARCHAR(64)
SET @ColName='intcolumn'
DECLARE @SQL NVARCHAR(1000)
SET @SQL = 'SELECT @intParam_out = ' + @ColName + ' FROM myTable'
exec sp_executesql @SQL, N'@intParam_out int OUTPUT', @intParam_out = @intParam OUTPUT
Use Cast:
SET @intParam = SELECT cast(@ColName as int) FROM myTable