I’m trying to change my WHERE clause based on the value of one column of my table.
My tables are:
server_updates
k1ID updID
----------- -----------
server_updates_cat
sucID updID catID selectChildren includeNullType
----------- ----------- ----------- ---------------- --------------
server_updates_types
sucID typeID
----------- -----------
And here’s what I’m doing:
DECLARE @k1_catID INT
DECLARE @k4_type INT
SET @k1_catID = 30
SET @k4_type = 1
SELECT suc.sucID, suc.updID FROM server_updates su
INNER JOIN server_updates_cat suc ON suc.updID = su.updID
WHERE
includeNullType = 0
AND suc.catID = @k1_catID
AND selectChildren = 1
AND @k4_type IN (SELECT typeID FROM server_updates_types sut WHERE sut.sucID = suc.sucID)
OR
includeNullType = 1
AND KSUC.catID = @k1_catID
AND selectChildren = 1
AND (@k4_type IN (SELECT typeID FROM server_updates_types sut WHERE sut.sucID = suc.sucID) OR @k4_type IS NULL)
I would like to not have to repeat all conditions either includeNullType = 0 or includeNullType = 1, but change the only thing I really need to change, which is:
@k4_type IN (SELECT typeID FROM server_updates_types sut WHERE sut.sucID = suc.sucID)
Is there another way of doing that?
1 Answer