SQL中的树型编号处理

1在朋友的程序里面看见这段代码,用于treeview生成新编号的
ContractedBlock.gif ExpandedBlockStart.gif 编码 #region 编码
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <param name="table">数据表</param>
InBlock.gif        
/// <param name="parentid">父id</param>
ExpandedSubBlockEnd.gif        
/// 通过以上参数求出下级目录的id

InBlock.gif        public string Id(string table,string parentid)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataSet dbset
=new DataSet();
InBlock.gif            
string sql="select * from "+table+" where parentid='"+parentid+"'";
InBlock.gif            dbset
=GetDs(sql);
InBlock.gif            
string s="";
InBlock.gif            
if (dbset.Tables[0].Rows.Count>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//已经存在子集
InBlock.gif
                string strid=dbset.Tables[0].Rows[0]["id"].ToString();
InBlock.gif                
int max=0;
InBlock.gif                max
=int.Parse(strid.Substring(1,strid.Length-1));
InBlock.gif                
for (int i=0;i<dbset.Tables[0].Rows.Count;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    strid
=dbset.Tables[0].Rows[i]["id"].ToString();
InBlock.gif                    
if (int.Parse(strid.ToString().Substring(1,strid.Length-1))>=max)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        max
=int.Parse(strid.Substring(1,strid.Length-1));
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                max
+=1;
InBlock.gif                
if (max.ToString().Length<strid.Length-1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
for (int i=0;i<strid.Length-1-max.ToString().Length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        s
=s+"0";
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                s
="a"+s+max.ToString();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//还没有子集
InBlock.gif
                if (parentid=="0")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//根目录
InBlock.gif
                    s="a01";
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//子目录
InBlock.gif
                    sql="select * from "+table+" where id='"+parentid+"'";
InBlock.gif                    dbset
=GetDs(sql);
InBlock.gif                    
if (dbset.Tables[0].Rows.Count>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
for (int i=0;i<int.Parse(dbset.Tables[0].Rows[0]["rank"].ToString());i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            s
=s+"0";
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        s
=parentid+s+"1";
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return s;
InBlock.gif
ExpandedSubBlockEnd.gif        }
闲的实在无聊就用存储过程写了下,感觉调用起来也挺好:)
None.gif create   procedure  sp_getId( @tname   nvarchar ( 100 ), @parentid   varchar ( 50 ))
None.gif
as
None.gif
begin
None.gif    
declare   @sql   nvarchar ( 4000 )
None.gif    
set   @sql = N ' select 
None.gif                 case @parentid when 
'' 0 ''  then  '' a ''  else @parentid end +
None.gif                     right(cast(POWER(10,isnull((select rank from 
' + @tname + N '  where id=@parentid),1)) as varchar(50))+rtrim(isnull(max(stuff(id,1,len(parentid), '''' )),0)+1),
None.gif                     case 
None.gif                       when isnull((select rank from 
' + @tname + N '  where id=@parentid),1)<2 then 2 
None.gif                       else 
None.gif                          case 
None.gif                            when not exists(select 1 from 
' + @tname + N '  where parentid=@parentid)
None.gif                              then (select rank from 
' + @tname + N '  where id=@parentid)+1 
None.gif                            else
None.gif                              (select rank from 
' + @tname + N '  where parentid=@parentid)
None.gif                          end
None.gif                     end) 
None.gif    from 
' + @tname + N '  where parentid=@parentid '
None.gif    
exec  sp_executesql  @sql , N ' @parentid varchar(50) ' , @parentid
None.gif
end
None.gif
GO
函数这么写:
None.gif create   function  f_getId( @parentid   varchar ( 50 ))
None.gif
returns   varchar ( 50 )
None.gif
as
None.gif
begin
None.gif    
declare   @id   varchar ( 50 )
None.gif    
select  
None.gif        
@id =   case   @parentid   when   ' 0 '   then   ' a '   else   @parentid   end   -- a01(表中无数据)/a06表中数据,是第一级
None.gif
             + right ( cast ( POWER ( 10 , isnull (( select  rank  from  notbm  where  id = @parentid ), 1 ))  as   varchar ( 50 )) + rtrim ( isnull ( max ( stuff (id, 1 , len (parentid), '' )), 0 ) + 1 ),
None.gif         
case   when  
None.gif
isnull (( select  rank  from  notbm  where  id = @parentid ), 1 ) < 2   then   2  
None.gif
else  
None.gif
case   when   not   exists ( select   1   from  notbm  where  parentid = @parentid )
None.gif
then   
None.gif(
select  rank  from  notbm  where  id = @parentid ) + 1   else
None.gif(
select  rank  from  notbm  where  parentid = @parentid )
None.gif 
end   end
None.gif    
from  notbm 
None.gif    
where  parentid = @parentid
None.gif    
return   @id
None.gif
end
None.gif
None.gif
GO
和程序差不多的SQL写法:
None.gif declare   @Result   as   varchar ( 50 )
None.gif
declare   @tmpChar   as   varchar ( 50 )
None.gif
declare   @iLen   as   Integer
None.gif
if   Exists ( select   1   from   [ table ]   where  parentid = ' [parentid] ' )
None.gif
-- 找得到parentid='parentid'的值
None.gif
   begin
None.gif    
select   @Result = max (id), @iLen = rank  from   [ table ]   where  parentid = ' [parentid] '   group   by  rank
None.gif
-- 相同parentid,其rank相同
None.gif
     select   @tmpChar = left ( @Result , len ( @result ) - @iLen )
None.gif    
select   @Result = ' 1 ' + right ( @Result , @iLen )
None.gif    
select   @Result = @tmpChar + right ( cast ( cast ( @Result   as   bigint ) + 1   as   varchar ( 50 )), @iLen )
None.gif  
end
None.gif
else
None.gif  
begin
None.gif    
if   [ parentid ] = ' 0 '
None.gif      
begin
None.gif        
select   @Result = ' a01 '
None.gif      
end
None.gif    
else
None.gif      
begin
None.gif        
select   @Result = id + replace ( space (rank), '   ' , ' 0 ' ) + ' 1 '   from   [ table ]   where  id = ' [parentid] '
None.gif      
end
None.gif  
end
None.gif
没头没尾,可能只有朋友看见了才懂我在说什么

转载于:https://www.cnblogs.com/kasafuma/archive/2006/03/30/362777.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值