2009-06-08 36 views
1

的UI(報告顯示之前)示出了一個查找(組合),其具有TSQL哈克需要用於獲取對數據的過濾器

  • (ID = 0)。所有組織單元
  • (ID = 4).HR
  • (ID = 5).DEV

我需要:

  • 如果選擇 (0),能夠顯示(4)+(5)的數據。
  • 僅限(4)或(5)如果選擇了HR或DEV。

  • 查找組合代碼(所選資訊在下面的查詢參數。)


    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?

    +1

    瞭解是ISNULL()和COALESCE(),因爲當您在WHERE,lookout表掃描中放置KEY = x OR Key = y時,「SET CONCAT_NULL_YIELDS_NULL OFF」將在未來 – 2009-06-08 15:15:13

    +0

    消失。 – 2009-06-09 20:32:52

    回答

    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))) 
    
    0

    這樣的事情應該工作

    Where (Employee.OrganizationUnit = case when @OrganizationUnit=0 then 4 else @OrganizationUnit end OR case when @OrganizationUnit=0 then 5 else @OrganizationUnit end) 
    
    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)); 
    
    +0

    關鍵字'BETWEEN'附近的語法不正確。 – abmv 2009-06-08 15:10:21

    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 ..... 
    
    0

    KM的版本將工作,但此查詢並不需要一個臨時表...

    SELECT * 
    FROM Employee 
    WHERE (
         @OrganizationUnit = 0 
         OR 
         (
          @OrganizationUnit <> 0 
          AND 
          Employee.OrganizationUnit = @OrganizationUnit 
         ) 
        )