[thelist] Conditionals with a SQL Server 2000 Stored Procedure

Paul Cowan evolt at funkwit.com
Tue Jun 4 18:50:00 CDT 2002


Hi Scott,

I'm no cold fusion guy, but I know SQL Server syntax.

SO, how about:

SELECT
    pc.blah,
    pc.blahblahblah
FROM
    dbo.whatever
WHERE
    (pc.category_id = c.category_id) AND
    (@category_ID = 0 OR p.category_id = @category_id)

You cannot conditionally add WHERE clauses as such; but you CAN
make it so that if @category_id is 0, then the second half
always evaluates as 'true'. (note parentheses).

No need for IFs or cases...

For what it's worth, IFs are more for conditional execution of code than
for in clauses. A CASE is closer, but still overkill in this situation.

Hope this helps!

Paul




More information about the thelist mailing list