当前位置: 代码迷 >> Sql Server >> 拜托各位大侠帮小弟我优化一个存储过程,
  详细解决方案

拜托各位大侠帮小弟我优化一个存储过程,

热度:104   发布时间:2016-04-27 21:48:06.0
拜托各位大侠帮我优化一个存储过程,急!!!
如题
===========================================================================
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   ([email protected]   or   b.lookuser= 'ALL '))  
            or   (a.DocumentType=c.typeID   and   ([email protected]   or   c.lookdept= 'ALL '))
            or(a.DocumentType=d.typeID   and   ([email protected]   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 [email protected]
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 [email protected]
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 [email protected]
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 [email protected] 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 [email protected] 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
  相关解决方案