why sp_ExecuteSql output parameter returns null when procedure shows correct value in results pane declare @Table nvarchar(20) = 'tblsteps';
declare @IdFieldName nvarchar(20) = 'stepid';
declare @NumberFieldName nvarchar(20) = 'stepnum';
declare @recid nvarchar(10) = 3;

declare @sql nvarchar(200);
declare @params nvarchar(500); 
declare @number_out nvarchar(20)

set @sql =  'SELECT ' +  @NumberFieldName + ' FROM ' + @table + ' WHERE ' + @IdFieldName +' = '+ @recid;

set @params= N'@number_out nvarchar(20) OUTPUT';

Execute sp_executesql @sql,@params,@number_out OUTPUT;

select @number_out as sp_Out;


select @sql as sqlString shows desired sql string;

Execute sp_executesql returned correct answer in results pane;

select @numberout as sp_out always returns NULL;
You never actually set the value of the output parameter in your query. In order to return the value as an output parameter you need to assign the result of the query to the parameter itself. 

Try this:

set @sql =  'SET @number_out = (SELECT ' +  @NumberFieldName 
                              + ' FROM ' + @table 
                             + ' WHERE ' + @IdFieldName +' = '+ @recid + ')';


Or you could do:

set @sql =  'SELECT @number_out = ' +  @NumberFieldName 
          + ' FROM ' + @table 
          + ' WHERE ' + @IdFieldName +' = '+ @recid;