【发布时间】:2009-12-22 20:32:13
【问题描述】:
我设计了 5 个存储过程,它们几乎使用相同的连接条件,但 where 子句中的参数或值在不同的运行中会发生变化。 最好的解决方案是创建一个没有 where 子句的所有连接条件的视图,然后从视图中查询或在视图上工作?如果我创建视图,视图可以自动更新吗? 我可以做子查询或类似的查询(我想我在某处读到的视图不支持子查询,但不是 100% 肯定)
select count(x1) as x1cnt, count(x2) as x2count
from (
select x1,x2,
(
case when x1 is 'y' then 1 else 0 end +
case when x2 is 'y' then 1 else 0 end
) per
from vw_viewname) v1
where v1.per = 1
更新如下:
In my queries i use joins similar to this also
select c1,c2,c3
FROM [[join conditions - 5 tables]]
Inner join
(
select x1,x2,x3, some case statements
FROM [[join conditions - 5 tables]]
where t1.s1 = val1 and t2.s2 = v2 etc
) s
on s.id = id
所以我使用了两次连接,所以我想我可以使用一些视图来减少它
【问题讨论】:
标签: sql sql-server sql-server-2005 stored-procedures views