如果@UserRoleId = 4,我需要在 WHERE 子句中添加一个条件,该条件将在搜索中包含项目,否则将它们从搜索中排除(SQL Server)。
INSERT INTO @partData
(
PartPrefix,
PartNumber,
MyProDescription,
ItemNumber,
PartType,
Side,
VehicleLocation,
MyProPrice
)
SELECT
CASE
WHEN LEFT(myParts.MyProDescription, 4) = 'FOIL' THEN 'FOIL'
ELSE LEFT(myParts.PartNumber, 3)
END,
PartNumber,
MyProDescription,
ItemNumber,
PartType,
Side,
VehicleLocation,
MyProPrice
FROM
MyProExport myParts
WHERE
@year BETWEEN myParts.YearFrom AND myParts.YearTo AND
myParts.CarMake = @make AND
REPLACE(myParts.CarModel, ' ', '') = REPLACE(@model, ' ', '') AND
(
myParts.EngineDisplacement = @engine OR
myParts.EngineDisplacement = 'ALL'
) AND
LEFT(myParts.PartNumber, 3) NOT IN ('300','400') -- Exclude these parts if not @UserRoleId = 4
只有当@UserRoleId = 4 时,最后一条语句才需要修改为显示 300 和 400。否则只显示 300。我对如何设置感到困惑。我试过了:
CASE
WHEN @UserRoleId = 4 THEN LEFT(myParts.Number, 3) NOT IN ('300','400') -- Exclude parts
没有运气...