[gray]-- 1) create a temporary copy of the table, including an additional field for the evaluation result[/gray]
[b]select[/b] *, [b]cast[/b]([b]null[/b] [b]as[/b] [b]float[/b]) replacetokens [b]into[/b] #temp [b]from[/b] ajaykumardev
[gray]-- 2) declare the necessary variables[/gray]
[gray]-- similar as in my previous code on 2 Dec 13 9:35[/gray]
[b]declare[/b]
@expression [b]varchar[/b](100) = [green][i]'(empcode+(rate*(perval*4)))'[/i][/green]
[b]declare[/b]
@query [b]nvarchar[/b](100) = [green][i]'select @result = '[/i][/green] + replace(replace(replace(@expression, [green][i]'empcode'[/i][/green], [green][i]'@empcode'[/i][/green]), [green][i]'rate'[/i][/green], [green][i]'@rate'[/i][/green]), [green][i]'perval'[/i][/green][green][/green], [green][i]'@perval'[/i][/green]),
@param [b]nvarchar[/b](100) = [green][i]'@empcode float, @rate float, @perval float, @result float out'[/i][/green],
@result [b]float[/b],
[gray]-- variables for the fetched values[/gray]
@id [b]int[/b],
@one [b]float[/b],
@two [b]float[/b],
@three [b]float[/b]
[gray]-- and a cursor to fetch from[/gray]
[b]declare[/b]
tempcursor [b]cursor[/b] [b]for[/b] [b]select[/b] id, one, two, three [b]from[/b] #temp
[gray]-- 3) loop over the records and perform the calculation for each[/gray]
[b]open[/b] tempcursor
[b]fetch[/b] [b]next[/b] [b]from[/b] tempcursor [b]into[/b] @id, @one, @two, @three
[b]while[/b] @@fetch_status = 0 [b]begin[/b]
[gray]-- evaluate the formula with the current values[/gray]
[b]exec[/b] sp_executesql @query, @param, @one, @two, @three, @result [b]out[/b]
[gray]-- put the resulted value into the result field[/gray]
[b]update[/b] #temp [b]set[/b] replacetokens = @result [b]where[/b] id = @id
[b]fetch[/b] [b]next[/b] [b]from[/b] tempcursor [b]into[/b] @id, @one, @two, @three
[b]end[/b]
[b]close[/b] tempcursor
[b]deallocate[/b] tempcursor
[gray]-- 4) finally query the values you always wanted from the temporary table[/gray]
[b]select[/b] * [b]from[/b] #temp