From: tshad on
I do this all the time but can't seem to see why this doesn't work

This gives me back 4 rows when @UserID is not null.

SELECT ChildGroupID = g.GroupID, ParentGroupID = NULL, Level=0
FROM UserGroup ug with (NOLOCK)
INNER JOIN Group] g with (NOLOCK) ON ug.GroupID = g.GroupID
WHERE (ug.UserID = @UserID AND g.Enabled = 1)

This one gives me back about 200 rows when @UserID is not null????

SELECT ChildGroupID = g.GroupID, ParentGroupID = NULL, Level=0
FROM UserGroup ug with (NOLOCK)
INNER JOIN Group] g with (NOLOCK) ON ug.GroupID = g.GroupID
WHERE @UserID IS NOT NULL OR (ug.UserID = @UserID AND g.Enabled = 1)

It should see the that the left side of the OR is False and do the right
side.

But now it comes up with way more rows?????

Since @UserID is NOT null, it is essentially the same statement, isn't it.

Thanks,

Tom


From: Eric Isaacs on
If the @UserID is NOT NULL, then the left side is always true, so
you'll get all rows returned.

> WHERE @UserID IS NOT NULL OR ...


-Eric Isaacs
From: Michael MacGregor on
No it is not the same thing, it's an OR statement, it will evaluate both
sides and return results accordingly, so by using

WHERE @UserID IS NOT NULL OR (ug.UserID = @UserID AND g.Enabled = 1)

you will get rows that satisfy BOTH conditions, hence the additional 196
rows.

Michael MacGregor


From: tshad on
You're right and I should have seen it.

What it needed to be was:

WHERE @UserID IS NOT NULL AND (ug.UserID = @UserID AND g.Enabled = 1)

Thanks,

Tom

"Michael MacGregor" <nospam(a)nospam.com> wrote in message
news:Ob7TrlryKHA.5288(a)TK2MSFTNGP05.phx.gbl...
> No it is not the same thing, it's an OR statement, it will evaluate both
> sides and return results accordingly, so by using
>
> WHERE @UserID IS NOT NULL OR (ug.UserID = @UserID AND g.Enabled = 1)
>
> you will get rows that satisfy BOTH conditions, hence the additional 196
> rows.
>
> Michael MacGregor
>
>