Problem : How to evaluate if statement or nested if statement or how we can evaluate particular expression given for calculation or evaluation. In c#.net it is vary difficult to evaluate expression having/with if statement.
Solution : We can use exiting functionality of Sql Server keyword Exec statement by little modification in it while evaluating the expression.
Step 1 Create Stored Procedure
CREATE procedure [dbo].[EvaluateExpression]
@Expression as varchar(1000),
@Result as varchar(500) output
Declare @TempTable Table (VariableValue varchar(500))
SET @Expression=(Select replace(@Expression,'==','='))
SET @Expression='if (0=1) Then 0 else ' + @Expression
SET @Expression = replace(@Expression,'Then','Select')
SET @Expression = replace(@Expression,'else','else Select')
SET @Expression = replace(@Expression,'Select if','if')
Insert into @TempTable exec (@Expression);
Select @Result=VariableValue from @TempTable;
Step 2 : Execute Stored Procedure
declare @v varchar(1000);
declare @formula as varchar(1000)
set @formula = 'IF(5=5) THEN IF(5=5) Then 10 ELSE 5'
exec [EvaluateExpression] @formula,@Result=@v output
Note : It will reduce efforts to write new evaluation engine in c# and but still we can pick the right things at right time as per our requirements.
Thanks for reading