1

UI(在报告显示之前)显示了一个查找(组合),它具有

  • (ID = 0).所有组织单位
  • (ID =4).HR
  • (ID = 5).DEV

我需要:

  1. 如果选择 (0),则能够显示 (4) + (5) 的数据。
  2. 如果选择 HR 或 DEV,则只有 (4) 或 (5)。

查找组合代码(在以下查询中选择提供参数。)


Select 0 AS ID,'All Org' AS Name from  DP_ORG_OrganizationUnit
where DP_ORG_OrganizationUnit.Code IN {AccessData}
Union
SELECT 
DP_ORG_OrganizationUnit.ID,
DP_ORG_OrganizationUnit.Name
FROM DP_ORG_OrganizationUnit  where DP_ORG_OrganizationUnit.Code IN ('HR','DEV')


报表数据行查询


SET CONCAT_NULL_YIELDS_NULL OFF

DECLARE @EmpID as int; 
DECLARE @OrganizationUnit as int; 
DECLARE @StartDate as datetime;
DECLARE @EndDate as datetime;

SET @EmpID = ?;
SET @StartDate = ?;
SET @EndDate = ?;
SET @OrganizationUnit = ?;

SELECT
Employee.Code,
Employee.Name1+' '+Employee.Name2+' '+Employee.Name3+' '+Employee.Name4+' '+Employee.Name5 AS FullName,
Employee.OrganizationUnit,  
ContractType.Name,
EmployeeContract.StartDate,
EmployeeContract.EndDate
FROM Employee INNER JOIN (ContractType INNER JOIN EmployeeContract 
ON ContractType.ID = EmployeeContract.ContractType) 
ON Employee.ID = EmployeeContract.Employee
WHERE (Employee.ID = @EmpID  OR  @EmpID=0)
AND
(Employee.OrganizationUnit = @OrganizationUnit  OR  @OrganizationUnit=0)
AND  NOT((EndDate <  @StartDate or StartDate > @EndDate)); 

有什么办法可以从它的外观上实现它?0=0 也会显示其他部门的所有数据..

任何人:-o?

4

5 回答 5

2

首先,您的查找组合代码可以收紧一点:

-- the FROM clause was superfluous
SELECT 0 AS ID,'All Org' AS Name 
UNION ALL
-- the two-part identifiers were superfluous (only one table)
SELECT ID, Name
FROM DP_ORG_OrganizationUnit
WHERE Code IN ('HR','DEV')

对于报告查询,最简单的形式是:

WHERE 
  ((@OrganizationUnit > 0 AND Employee.OrganizationUnit = @OrganizationUnit) OR 
   (@OrganizationUnit = 0 AND Employee.OrganizationUnit IN (4,5)))
于 2009-06-08T23:46:54.340 回答
0

像这样的东西应该工作

Where (Employee.OrganizationUnit = case when @OrganizationUnit=0 then 4 else @OrganizationUnit end OR case when @OrganizationUnit=0 then 5 else @OrganizationUnit end)
于 2009-06-08T14:44:06.577 回答
0

怎么样

WHERE (Employee.ID = @EmpID  OR  @EmpID=0)
AND
(Employee.OrganizationUnit BETWEEN ISNULL(NULLIF(@OrganizationUnit,0),0) AND ISNULL(NULLIF(@OrganizationUnit,0),99))
AND  NOT((EndDate <  @StartDate or StartDate > @EndDate));
于 2009-06-08T14:51:20.900 回答
0

试试这个,它应该在你的查询中使用索引......

DECALRE @FilterValues (FilterValue   int not null primary key)

IF @Param=0
BEGIN
    INSERT INTO @FilterValues VALUES (4)
    INSERT INTO @FilterValues VALUES (5)
END
ELSE ID @PAram IS NOT NULL
BEGIN
    INSERT INTO @FilterValues VALUES (@Param)
END

SELECT
    ....
    FROM YourTable                y
        INNER JOIN @FilterValues  f ON y.Value=f.Value
    WHERE .....
于 2009-06-08T15:10:30.440 回答
0

KM 的版本可以工作,但是这个查询不需要临时表......

SELECT *
FROM Employee
WHERE (
         @OrganizationUnit = 0
         OR 
         (
             @OrganizationUnit <> 0
             AND
             Employee.OrganizationUnit = @OrganizationUnit
         )
      )
于 2009-06-09T00:22:40.587 回答