Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase

Evaluating Boolean expressions using T-SQL Expand / Collapse
Posted Monday, November 29, 2004 1:06 PM


Group: General Forum Members
Last Login: Tuesday, November 17, 2015 6:13 AM
Points: 143, Visits: 302
Comments posted to this topic are about the content posted at

Post #148433
Posted Wednesday, December 22, 2004 3:24 AM
Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Today @ 7:48 AM
Points: 693, Visits: 129

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.

Post #152125
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse