DECLARE @cityID bigint;
set @cityID = NULL
--set @cityID = 3
SELECT ID,
Name,
CityID,
IsProvince
FROM TABLE t
WHERE ISNULL(t.CityID, -1) = ISNULL(@cityID, -1)
whenever the @cityID is NULL the t.CityID is also NULL for that record.
Only one record can ever be true for IsProvince
Is there a way to check without doing the if-else clause if I want to do the following:
1) If the @cityID is NULL, then get the record whose IsProvince is 1
2) If the @cityID is NOT NULL then get the record whose CityID = @cityID
Thanks,
Voodoo
You can do this in the WHERE:
This should work since anything = NULL always returns false. The first clause should solve your #1, and the second solves #2. I’m not sure if you really need the
t.CityID IS NULLclause though, since your criteria for #1 doesn’t mention it.Does this make sense?