1 Star 0 Fork 737

codingstudio/CCFlow

forked from 济南驰骋公司/CCFlow 
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
GO.sql 2.88 KB
一键复制 编辑 原始数据 按行查看 历史
ccflow..liyan 提交于 2018-06-08 09:05 +08:00 . no commit message
/** 以下代码由孙战平 2013.07.19 */
--GO--
Create FUNCTION [dbo].[f_FixNumLen]
(
@str nvarchar(200),
@len int
)
RETURNS nvarchar(200)
AS
BEGIN
if(@len>len(@str))
set @str = replicate('0',@len-len(@str))+@str;
return @str;
END
--GO--
Create PROCEDURE [dbo].[sp_get_tree_table]
(@table_name nvarchar(200), @id nvarchar(50), @name nvarchar(50), @parent_id nvarchar(50) , @orderBy nvarchar(50), @startParentId nvarchar(20),@is_top bit, @FixLenFlag int)
AS
declare @v_affect int
declare @v_level int
declare @sql nvarchar(500)
begin
create table #temp (id nvarchar(20),name nvarchar(50),parent_id nvarchar(20),level int,treespace nvarchar(500),orderid int);
declare @strTable varchar(200);
set @table_name=LTRIM(@table_name);
if(charindex('from ',lower(@table_name))=1)
set @strTable='select * '+@table_name;
else
Begin
if(charindex('select ',lower(@table_name))<>1 and charindex(' where ',lower(@table_name))>1)
set @strTable='select * from '+@table_name;
else
set @strTable=@table_name;
End
if(charindex('select ',lower(@strTable))=1)
set @strTable='('+@strTable+')'
if(charindex('(select ',lower(@strTable))=1)
set @sql='insert into #temp(id,name,parent_id,orderid) select '+@id+','+@name+','+@parent_id +','+@orderBy+' from ' + @strTable +' as A';
else
set @sql='insert into #temp(id,name,parent_id,orderid) select '+@id+','+@name+','+@parent_id +','+@orderBy+' from ' + @strTable +'';
exec sp_executesql @sql;
set @v_level=1;
set @v_affect=1;
while(@v_affect>0)
Begin
if @v_level=1
if @startParentId is null
update #temp
set level=@v_level,treespace=dbo.f_FixNumLen(Cast(orderid as varchar),@FixLenFlag) +'-'+dbo.f_FixNumLen(Cast(id as varchar),@FixLenFlag)
where parent_id is null;
else
update #temp
set level=@v_level,treespace=dbo.f_FixNumLen(Cast(orderid as varchar),@FixLenFlag) +'-'+dbo.f_FixNumLen(Cast(id as varchar),@FixLenFlag)
where parent_id = CAST(@startParentId as int);
else
update #temp
set level=@v_level,treespace=(select A.treespace from #temp A where A.id=#temp.parent_id)+'-'+dbo.f_FixNumLen(Cast(orderid as varchar),@FixLenFlag) +'-'+dbo.f_FixNumLen(Cast(id as varchar),@FixLenFlag)
where parent_id in (select id from #temp A where A.level=@v_level-1);
set @v_affect=@@ROWCOUNT;
set @v_level=@v_level+1;
End
delete from #temp where level is null;
if @is_top=1
select id,(case when level>1 then REPLICATE('│',level-2) else '' end)+(case when level>1 then '├' else '' end) + name as name
from #temp order by treespace;
else
select id,REPLICATE('│',level-1) + '├' + name as name
from #temp order by treespace;
end
--GO--
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/codingstudio/ccflow.git
git@gitee.com:codingstudio/ccflow.git
codingstudio
ccflow
CCFlow
develop

搜索帮助