0

如果@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

没有运气...

4

1 回答 1

1

而不是 case 语句,只需使用 AND/OR 逻辑。

AND (LEFT(myParts.HollanderNumber, 3) = '300'
OR (@UserRoleID = 4 AND LEFT(myParts.HollanderNumber, 3) = '400'))

这将始终显示 300,并且仅显示 400,如果@UserRoleID = 4

于 2013-02-25T16:45:07.133 回答