Calculate by formula in column
1 answer
This may not be a complete answer, but may help you well
Declare @tab table (pk int,formula varchar(100),OP_NME varchar(10),X int)
Insert into @tab values(1,'x*10','A',Null),(2,'(X+Y)*10','B',Null)
Declare @result table (id int identity(1,1),[values] int,pk int)
Declare @s int = 1,
@e int = (Select max(pk) From @tab),
@sql varchar(max) = 'Declare @x int = 10,@y int = 20 ',
@sql2 varchar(max)
While @s <= @e
Begin
Select @sql2 = @sql + 'Select(' + Replace(Replace(formula,'x','@x'),'y','@y') + ')' From @tab Where pk = @s
Insert into @result ([values])
Exec(@sql2)
Update @result
Set pk = @s
Where id = @@identity
Set @s = @s + 1
End
Select T.pk,T.formula,R.[values]
from @result R
join @tab T On R.pk = T.pk
Run SQL in management studio and see the result:
+4
source to share