zoukankan      html  css  js  c++  java
  • sql语句总结一 dodo

    1、查找某一个部门的所有上级部门节点信息

    ALTER function [dbo].[fnGetFatherGroup](@id varchar(50)) 
    returns @t table(GroupID varchar(50),G_ParentID varchar(50),G_Level int,G_IsCompany int) 
    as 
    begin 
    insert into @t select GroupID,G_ParentID,G_Level,G_IsCompany from sys_Group where GroupID = @id 
    select @id = G_ParentID from sys_Group where GroupID = @id 
    while @@rowcount > 0 
    begin 
    insert into @t select GroupID,G_ParentID,G_Level,G_IsCompany from sys_Group where GroupID = @id 
    select @id = G_ParentID from sys_Group where GroupID = @id 
    end 
    return 
    end 

    2、查找表中某一列最大值的信息

    select a.GroupID from fnGetFatherGroup(56) a where G_Level = ( select max(G_Level) from fnGetFatherGroup(56) where G_IsCompany = 1 ) 

    3、函数用于查找字符串中重复出现的字符

    ALTER FUNCTION [dbo].[fnGetDistinctStr](@S NVARCHAR(100),@SPLIT VARCHAR(50))
    RETURNS VARCHAR(100)
    AS
    BEGIN
    IF @S IS NULL RETURN(NULL)
    DECLARE @NEW VARCHAR(50),@INDEX INT,@TEMP VARCHAR(50)
    IF LEFT(@S,1)<>@SPLIT
    SET @S = @SPLIT+@S
    IF RIGHT(@S,1)<>@SPLIT
    SET @S = @S+@SPLIT
    WHILE CHARINDEX(@SPLIT,@S)>0 AND LEN(@S)<>1
    BEGIN
    SET @INDEX = CHARINDEX(@SPLIT,@S)
    SET @TEMP = LEFT(@S,CHARINDEX(@SPLIT,@S,@INDEX+LEN(@SPLIT)))
    IF @NEW IS NULL 
    SET @NEW = ISNULL(@NEW,'')+@TEMP
    ELSE
    SET @NEW = ISNULL(@NEW,'')+REPLACE(@TEMP,@SPLIT,'')+@SPLIT
    WHILE CHARINDEX(@TEMP,@S)>0
    BEGIN
    SET @S=STUFF(@S,CHARINDEX(@TEMP,@S)+LEN(@SPLIT),CHARINDEX(@SPLIT,@S,CHARINDEX(@TEMP,@S)+LEN(@SPLIT))-CHARINDEX(@TEMP,@S),'')
    END
    END
    RETURN RIGHT(LEFT(@NEW,LEN(@NEW)-1),LEN(LEFT(@NEW,LEN(@NEW)-1))-1)
    END

    4、用于查找某一个子字符串在整个字符串的出现次数

    ALTER FUNCTION [dbo].[fnGetRepeatCharNum]
    (    
        @CodeString varchar(2000),
        @RepeatString varchar(200)
    )
    RETURNS int 
    AS
    BEGIN 
        DECLARE
            @RepeatStringLen int,    --检测重复字符的长串
            @TempString varchar(500)    --临时字符串(总比@RepeatString多一个即长度大1)
        
        SET @RepeatStringLen=LEN(@RepeatString)
        SET @TempString=Replicate(',',@RepeatStringLen)+','
    
        RETURN LEN(REPLACE(@CodeString,@RepeatString,@TempString))-LEN(@CodeString)
    END

    5、用于显示数据表某一列的所有内容相加

    select top 1 userid, (select ','+ltrim(u_cname) from sys_User for xml path('')) as allu_cname from sys_User b  

    6、查找相邻节点

     //select top 1 GroupID from sys_Group where G_ShowOrder < 3 and G_Level = 3 and G_ParentID = 221 order by G_ShowOrder asc 当前节点的上一个节点
     //select top 1 GroupID from sys_Group where G_ShowOrder > 3 and G_Level = 3 and G_ParentID = 221 order by G_ShowOrder asc 当前节点的下一个节点
  • 相关阅读:
    vs2019 临时下载目录 离线安装 脱机安装
    直膝跳缓解腰椎间盘突出 腰疼 臀部肌肉疼痛
    fullscreen
    ubuntu 18.04 网络图标不见的问题解决方案
    采样率与比特率
    关于git的换行符问题
    在ASP.NET Core中使用EPPlus导入出Excel文件
    .net core 转 excel datatable list<t> 互转 xlsx
    .net core C# DataTable 和List之间相互转换的方法
    C# 的三种序列化方法
  • 原文地址:https://www.cnblogs.com/zgqys1980/p/2803047.html
Copyright © 2011-2022 走看看