我的工作是维护一个大量使用 SQL server (MSSQL2005) 的应用程序。
到目前为止,中间服务器将 TSQL 代码存储在 XML 中,并在不使用存储过程的情况下发送动态 TSQL 查询。
因为我能够更改那些 XML 查询,所以我想将我的大部分查询迁移到存储过程。
问题如下:
我的大多数查询对一张表都有相同的 Where 条件
样本:
Select
.....
from ....
where ....
and (a.vrsta_id = @vrsta_id or @vrsta_id = 0)
and (a.podvrsta_id = @podvrsta_id or @podvrsta_id = 0)
and (a.podgrupa_2 = @podgrupa2_id or @podgrupa2_id = 0)
and (
(a.id in (select art_id from osobina_veze where podosobina_id in (select ado from dbo.fn_ado_param_int(@podosobina))
group by art_id
having count(art_id)= @podosobina_count ))
or ('0' = @podosobina)
)
它们在其他表上也具有相同的 where 条件。
我应该如何组织我的代码?
什么是正确的方法?
我应该创建将在所有查询中使用的表值函数,
还是在每次执行 proc 时使用 #Temp 表和简单的内部连接我的查询?或使用表值函数提交的#temp?
或者将所有查询都保留在这个大的 where 子句中,并希望索引能够完成它们的工作。
或使用 WITH(语句)