Hi,
as i said before, you can use sp_executesql. read bol for details...
you have to distinguish between vars and their values. your "select @colname from test" will select whatever is the value of colname, regardless the table you specify! even without a table name the cmd will show your var value.
Code:
declare @Table sysname, @ID int, @ColName nvarchar(200), @CMD nvarchar(1000), @ColData varchar(20)
select @Table='test', @ID=5
-- get column name for table / colid
select @ColName=name from syscolumns where colid=@ID and id= object_id(@Table)
-- build up cmd string for pulling data
select @CMD='select @Param = '+@ColName+' from '+@Table
-- show what we have so far
print '@Table = >'+@Table+'<'
print '@COLNAME = >'+@ColName+'<'
print '@CMD= >'+@CMD+'<'
-- execute the cmd string pulling data from @colname into @ColData via @Param
exec sp_executesql @CMD, N'@Param varchar(20) output', @Param = @ColData output
-- here we are
print '@COLDATA= >'+@COLDATA+'<'