The first part of the procedure is good enough, but a better solution could be done for the final evaluation. Instead of using a temporary table, we can use output parameters in the sp_executesql procedure, like this:

This way, we replace about 5 lines of code with only 2 and avoid the overhead of using a temporary table. To use the sp_executesql procedure we need to change the type of the @boolExp parameter to nvarchar(300) instead of varchar(300).

Razvan

PS. The SET NOCOUNT OFF and the DROP TABLE #temp at the end of the procedure are useless (because they are done anyway, automatically). Also useless is the big BEGIN/END block that contains the entire procedure.