日期:2014-05-18  浏览次数:20472 次

关于存储过程的性能,请各位帮帮忙.
--   存储过程
CREATE   PROCEDURE   dbo.usp_SearchProductByCount(
@AncestorCatalogId   int,
@qTables   varchar(1000),               --需要查询的表
@qSqlstr   varchar(1000) --条件
)
as
begin
SET   NOCOUNT   ON

--   目录树临时表
CREATE   TABLE   #CatalogTree(
CatalogId   int,
Generation   int)
DECLARE  
@Generation   int  
declare   @idlist   varchar(4000)
set   @idlist= ' '
SET   @Generation   =   1

INSERT   INTO   #CatalogTree  
SELECT   @AncestorCatalogId,   @Generation  

DECLARE   @sqlTable   AS   varchar(2000)
DECLARE   @Recordstr   AS   VARCHAR(2000)
WHILE   @@RowCount   >   0
BEGIN
SET   @Generation   =   @Generation   +   1
INSERT   #CatalogTree
SELECT   c.[TID],   @Generation
FROM   mainProductType   c
JOIN   #CatalogTree   t
ON   t.Generation   =   @Generation   -   1   AND   t.CatalogId   =   c.SuperiorType  
END
set   @Recordstr   =   'select   count(*)   as   record     FROM   Products   '+@qTables+ '   WHERE   (MainType   IN   (SELECT   CatalogId   FROM   #CatalogTree)) '   +   @qSqlstr
exec   (@Recordstr)
SET   NOCOUNT   OFF
RETURN
end
GO

这是我执行返回记录总数,   用到的临时表,但用好后,却不知道如何清除,以致于占用内存,请高手们帮帮忙..

谢谢!~     #CatalogTree

------解决方案--------------------
在后面加上drop table #CatalogTree