日期:2014-05-17 浏览次数:20494 次
Repeater1.DataSource = db.ExecuteReader(connectionString, CommandType.StoredProcedure, "sp_Util_Sort_SELECT", New SqlParameter("@sField ", "SortID,SortName,SortParentID,SortParentPath,SortOrder"), New SqlParameter("@sTable ", "tbTempSort"), New SqlParameter("@iSortID ", 4), New SqlParameter("@iCond", 1)) Repeater1.DataBind()
/* 输出目录树 调用示范:EXEC sp_Util_Sort_SELECT 'SortID,SortName,SortParentID,SortParentPath,SortOrder','tbTempSort',4,1 输入: 1.字段名(字段名不能为SortNameTree,SortMoveUp,SortMoveDown) 2.表名 3.欲调用的分类ID(0或者此ID不存在,即调用所有) 4.条件(1包含本级ID和所有下级ID,2包含本级ID和子ID,3不包含本级ID的所有下级ID,4不包含本级ID的子ID) 返回:记录集 */ CREATE PROCEDURE sp_Util_Sort_SELECT ( @sField varchar(255), @sTable varchar(50), @iSortID int, @iCond tinyint ) AS BEGIN SET NOCOUNT ON DECLARE @s nvarchar(4000),@s1 varchar(1000),@s2 varchar(1000) SET @s1=CAST(@iSortID AS varchar(10)) SELECT @sField=(CASE WHEN LEN(@sField)>0 THEN @sField+',' ELSE '' END) IF @iSortID>0 BEGIN SET @s='SELECT @s2=SortParentPath FROM '+@sTable+' WHERE SortID='+@s1 EXEC sp_executesql @s,N'@s2 varchar(4000) OUT',@s2 OUT IF @s2 IS Null GOTO step1 ELSE GOTO step2 END ELSE BEGIN GOTO step1 END step1: BEGIN SELECT @s2=',' SELECT @s1=(CASE @iCond WHEN 2 THEN ' WHERE SortParentID=0' WHEN 4 THEN ' WHERE SortParentID=0' ELSE '' END) GOTO step3 END step2: BEGIN SELECT @s1=(CASE @iCond WHEN 2 THEN ' WHERE SortID='+@s1+' OR SortParentID='+@s1 WHEN 3 THEN ' WHERE CHARINDEX('','+@s1+','',SortParentPath)>0' WHEN 4 THEN ' WHERE SortParentID='+@s1 ELSE ' WHERE SortID='+@s1+' OR CHARINDEX('','+@s1+','',SortParentPath)>0' END) GOTO step3 END step3: SET @s='SELECT '+@sField+' dbo.fn_Sort_Tree(SortName,SortParentPath,'''+@s2+''',(SELECT COUNT(0) FROM '+@sTable+' B WHERE B.SortParentID=A.SortParentID AND B.SortOrder>A.SortOrder)) AS SortNameTree,(SELECT COUNT(0) FROM '+@sTable+' B WHERE B.SortParentID=A.SortParentID AND B.SortOrder<A.SortOrder) AS SortMoveUp,(SELECT -COUNT(0) FROM '+@sTable+' B where B.SortParentID=A.SortParentID AND B.SortOrder>A.SortOrder) AS SortMoveDown FROM '+@sTable+' A '+@s1+' ORDER BY SortOrder ASC' EXEC (@s) END GO