拜托各位大侠帮我优化一个存储过程,急!!!
如题
===========================================================================
CREATE PROCEDURE selectAllOpenDocumentsInfo
@user varchar(50),@dept varchar(50),@lv varchar(50)
AS
select a.documentID,a.documentName,e.typeName,a.documentInfo,a.VesionIDUser,a.vesionId,a.vesionDate,a.documentSize,a.documentSend,a.documenttype,
a.documenturl
from FeiliOA_DocumentManage_DocumentInfo a,FeiliOA_DocumentManage_TypeLookUser b,
FeiliOA_DocumentManage_TypeLookDept c,FeiliOA_DocumentManage_TypeLookLv d,
FeiliOA_DocumentManage_TypeInfo e
where ((a.DocumentType=b.typeID and (b.lookuser=@user or b.lookuser= 'ALL '))
or (a.DocumentType=c.typeID and (c.lookdept=@dept or c.lookdept= 'ALL '))
or(a.DocumentType=d.typeID and (d.looklv=@lv or d.looklv= 'ALL '))) and e.typeid=a.documenttype and a.documentStatic= 'a '
group by a.documentID,a.documentName,e.typeName,a.documentInfo,a.VesionIDUser,a.vesionId,a.vesionDate,a.documentSize,a.documentSend,
a.documenttype,a.documenturl
------解决方案--------------------CREATE PROCEDURE selectAllOpenDocumentsInfo
@user varchar(50),
@dept varchar(50),
@lv varchar(50)
--建临时表整理带 'all '字样的数据与变量到一个集合中
CREATE TABLE #FeiliOA_DocumentManage_TypeLookUser(typeID INT,lookuser VARCHAR(50))
INSERT INTO #FeiliOA_DocumentManage_TypeLookLv
SELECT typeID,lookuser FROM FeiliOA_DocumentManage_TypeLookLv WHERE lookuser=@user
UNION ALL
SELECT typeID,CASE WHEN lookuser= 'ALL ' THEN @user END FROM FeiliOA_DocumentManage_TypeLookLv
--
CREATE TABLE #FeiliOA_DocumentManage_TypeLookDept(typeID INT,lookdept VARCHAR(50))
INSERT INTO #FeiliOA_DocumentManage_TypeLookDept
SELECT typeID,lookdept FROM FeiliOA_DocumentManage_TypeLookDept WHERE lookdept=@dept
UNION ALL
SELECT typeID,CASE WHEN lookdept= 'ALL ' THEN @dept END FROM FeiliOA_DocumentManage_TypeLookDept
--
CREATE TABLE #FeiliOA_DocumentManage_TypeLookLv(typeID INT,looklv VARCHAR(50))
INSERT INTO #FeiliOA_DocumentManage_TypeLookLv
SELECT typeID,looklv FROM FeiliOA_DocumentManage_TypeLookLv WHERE looklv=@lv
UNION ALL
SELECT typeID,CASE WHEN looklv= 'ALL ' THEN @lv END FROM FeiliOA_DocumentManage_TypeLookLv
--将表关联
SELECT a.documentID,a.documentName,e.typeName,a.documentInfo,a.VesionIDUser,a.vesionId,a.vesionDate,a.documentSize,a.documentSend,a.documenttype,a.documenturl
FROM
FeiliOA_DocumentManage_DocumentInfo a INNER JOIN #FeiliOA_DocumentManage_TypeLookUser b
ON a.DocumentType=b.typeID AND b.lookuser=@user AND a.documentStatic= 'a ' INNER JOIN FeiliOA_DocumentManage_TypeInfo e
ON e.typeid=a.documenttype
UNION ALL
SELECT a.documentID,a.documentName,e.typeName,a.documentInfo,a.VesionIDUser,a.vesionId,a.vesionDate,a.documentSize,a.documentSend,a.documenttype,a.documenturl
FROM
FeiliOA_DocumentManage_DocumentInfo a INNER JOIN
FeiliOA_DocumentManage_TypeLookDept c
ON a.DocumentType=c.typeID and c.lookdept=@dept AND a.documentStatic= 'a ' INNER JOIN FeiliOA_DocumentManage_T