Evaluating Boolean expressions using T-SQL

  • Comments posted to this topic are about the content posted at http://www.sqlservercentral.com

  • 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:

    SET @boolExp='SELECT @res=CASE ABS(' + @boolExp + ') WHEN 1 THEN ''TRUE'' ELSE ''FALSE'' END'
    EXEC sp_executesql @boolExp, N' @res varchar(5) OUTPUT', @res OUTPUT

    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).


    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.

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply