现在已经完成了一段任务,而我的智慧结束了。我遇到的问题是我需要在一个参数中有一个表中的多个列,以便根据人的姓名缩写来提取报告。这就是我所拥有的。
WHERE (tblWarehouse.Employee + tblWarehouse.Employee2 +tblWarehouse.Employee3 =
CASE
WHEN ISNULL(@Employee, 'ALL') = 'ALL' THEN
tblWarehouse.Employee + tblWarehouse.Employee2 + tblWarehouse.Employee3
ELSE @Employee
END)
当我运行查询时,它会在我输入已在第一列和第二列中输入的员工姓名首字母时提取信息。但是,它没有从第三列中提取任何内容。是否有其他方法可以获取此信息?提前知道你。
答案 0 :(得分:2)
我认为你正试图这样做?
WHERE
'ALL' = @Employee
OR tblWarehouse.Employee = @Employee
OR tblWarehouse.Employee2 = @Employee
OR tblWarehouse.Employee3 = @Employee
请记住,这将无法使用单个索引来查找结果。理性是长篇大论,但以下可能更有效...
SELECT
blah
FROM
(
SELECT * FROM tblWarehouse WHERE 'All' = @employee
UNION ALL
SELECT * FROM tblWarehouse WHERE Employee = @employee
UNION ALL
SELECT * FROM tblWarehouse WHERE Employee2 = @employee
UNION ALL
SELECT * FROM tblWarehouse WHERE Employee3 = @employee
)
AS tblWarehouse
INNER JOIN
whatever
...
然后,这可以为每个UNIONed查询使用不同的索引,避免每次都扫描整个表,而只是使用快速搜索索引。