语句中使用情况下,如果在查询(SQL 2008)使用情况下,如果在查询
语句我有,每3个实例的服务器。我正在做其中一个查询在其上运行。部分查询将过滤每个服务器的ID,即对于server1为123。
select COUNT(*) from Department Dep
where Dep.LocationID in
(1,2,3, -- Server1
4,5,6, -- Server2
7,8,9 --Server3
)
我可以像上面那样过滤所有id并获得结果。由于表格非常大(2 miilon行),因此最好过滤tabel中的内容,即server1的123。虽然它完成所需的时间并没有太大的区别。
我能做到这一点
if @@SERVERNAME ='Server1'
Begin
select COUNT(*) from Department Dep
where dep.LocationID in
(1,2,3)
End
if @@SERVERNAME ='Server2'
Begin
select COUNT(*) from Department Dep
where dep.LocationID in
(4,5,6)
End
if @@SERVERNAME ='Server3'
Begin
select COUNT(*) from Department Dep
where dep.LocationID in
(7,8,9)
End
所以,如果我在Server2上运行上面的查询,我将只过滤(4,5,6)
我想加入这个结果到另一个查询因此寻找更好的方法来做到这一点。
感谢
很难说出你在这里问什么。你能澄清吗? – Codeman 2014-12-02 20:47:17
做三个查询并将结果插入表变量(或临时表),然后对表变量进行最终选择。 – 2014-12-02 20:51:21
对不起,希望它现在更清晰 – Rosebud 2014-12-02 21:05:57