TSQL Hack nécessaire pour obtenir un filtre pour les données
Question
Une interface utilisateur (avant que le rapport montre) montre un coup d'oeil vers le haut (Combo) qui a
- (ID = 0) Unités .Toutes Organisation
- (ID = 4) .HR
- (ID = 5) .DEV
Je dois:
- Pouvoir afficher les données de (4) + (5) (0) est sélectionné.
- Seulement (4) ou (5) si l'une des ressources humaines ou DEV est sélectionné.
Recherche code combo (le paramètre sélectionné Flux dans la requête ci-dessous.)
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')
données Rapport requête de ligne
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));
De toute façon je peux réaliser des regards de celui-ci? 0 = 0 montreraient toutes les données d'autres départements trop ..
Tout le monde: -o
La solution
Tout d'abord, votre code combo de recherche pourrait être serré un peu:
-- 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')
Pour la requête de rapport, la forme la plus simple serait:
WHERE
((@OrganizationUnit > 0 AND Employee.OrganizationUnit = @OrganizationUnit) OR
(@OrganizationUnit = 0 AND Employee.OrganizationUnit IN (4,5)))
Autres conseils
chose comme cela devrait fonctionner
Where (Employee.OrganizationUnit = case when @OrganizationUnit=0 then 4 else @OrganizationUnit end OR case when @OrganizationUnit=0 then 5 else @OrganizationUnit end)
Essayez ceci, qui devrait utiliser les index sur votre requête ...
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 .....
La version de KM fonctionnera, mais cette requête n'a pas besoin d'une table temp ...
SELECT *
FROM Employee
WHERE (
@OrganizationUnit = 0
OR
(
@OrganizationUnit <> 0
AND
Employee.OrganizationUnit = @OrganizationUnit
)
)
Qu'en est-
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));