我需要在WHERE子句中添加一个条件,如果@UserRoleId = 4,则在搜索中包含项目,否则将它们从搜索中排除(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
没有运气......
答案 0 :(得分:1)
而不是case语句,只需使用AND / OR逻辑。
AND (LEFT(myParts.HollanderNumber, 3) = '300'
OR (@UserRoleID = 4 AND LEFT(myParts.HollanderNumber, 3) = '400'))
这将始终显示300,如果@UserRoleID = 4