Re: If Statement to determine WHERE condition in SQL Query



In general, you'd use a CASE statement for such requirements, for instance:

SELECT Forename, Username, Description
FROM Audit
WHERE Date >= @Param_StartDate
AND Date < @Param_EndDate
AND UserID = CASE WHEN @Param_UserKey <> -1
THEN @Param_UserKey
ELSE UserID
END ;

If the column is nullable, you will have to use COALESCE or perhaps re-write
it as:

...AND ( @Param_UserKey <> -1 AND UserID = @Param_UserKey )
OR ( @Param_UserKey <> -1 )

And there are several other approaches for such requirements, some of which
are detailed at:
http://www.sommarskog.se/dyn-search.html

--
Anith


.



Relevant Pages

  • Re: Intrusion attempts
    ... I would guess that you are not yet hitting the breakin limit ... showed up in the audit record as a breakin attempt with both the ... username and password showing. ... Weaver Consulting Services Inc. ...
    (comp.os.vms)
  • Re: Help with Allen Browns (wonderful!) audit trail code.
    ... You will need to add the fields to both the audit table, ... Then you will need to modify the Append query statements so they include ... > trail code I got from you, ... >>> audit occurred, and the network username of the person who inserted, ...
    (microsoft.public.access.modulesdaovba)
  • Re: Help with Allen Browns (wonderful!) audit trail code.
    ... I'm not sure how to incorporate it into the audit ... trail code I got from you, ... > Not sure what you mean by the network full name. ... >> audit occurred, and the network username of the person who inserted, ...
    (microsoft.public.access.modulesdaovba)