• sqlserver常用表值函数


     1.fnSplit:把字符串分割为表。

    CREATE FUNCTION fnSplit
    (
    	@c         VARCHAR(2000),
    	@split     VARCHAR(2)
    )
    RETURNS @t TABLE(col VARCHAR(200))
    AS
    BEGIN
    	WHILE (CHARINDEX(@split, @c) <> 0)
    	BEGIN
    	    INSERT @t
    	      (
    	        col
    	      )
    	    VALUES
    	      (
    	        SUBSTRING(@c, 1, CHARINDEX(@split, @c) -1)
    	      )   
    	    SET @c = STUFF(@c, 1, CHARINDEX(@split, @c), '')
    	END   
    	INSERT @t
    	  (
    	    col
    	  )
    	VALUES
    	  (
    	    @c
    	  ) 
    	RETURN
    END
    GO
    
    
    

    2.fnGetSplitString  

    将字符串拆分为表:例子:“1,2,3,4,5”拆分为表

    CREATE FUNCTION [dbo].[fnGetSplitString]
    (
    	@strId      NVARCHAR(MAX),
    	@Delimiter  CHAR(1)
    )
    RETURNS @TableList TABLE(strId NVARCHAR(MAX))
    
    BEGIN
    	IF @strId = ''
    	    RETURN
    	
    	DECLARE @XML XML
    	SET @XML = '<root><csv>' + REPLACE(@strId, @Delimiter, '</csv><csv>') +
    	    '</csv></root>' 
    	
    	INSERT @TableList
    	SELECT RTRIM(
    	           LTRIM(REPLACE(Word.value('.', 'nvarchar(max)'), CHAR(10), ''))
    	       ) AS ListMember
    	FROM   @XML.nodes('/root/csv') AS WordList(Word)
    	
    	RETURN
    END
    
    
    GO
    
    
    

    3.fnGetSplitIdAndRversion

    分割“1:123,2:234,3:345”字符串为表

    CREATE FUNCTION [dbo].[fnGetSplitIdAndRversion]
    (	
    	@idrversionStr NVARCHAR(MAX)
    )
    RETURNS @table TABLE(id NVARCHAR(64),rversion BIGINT)
    BEGIN
    	IF LEN(@idrversionStr)>=3
    	BEGIN
    		SET @idrversionStr=RTRIM(LTRIM(@idrversionStr));
    		IF SUBSTRING(@idrversionStr,1,1)=','
    		BEGIN
    			SET @idrversionStr=SUBSTRING(@idrversionStr,1,LEN(@idrversionStr)-1)
    		END
    		IF SUBSTRING(@idrversionStr,LEN(@idrversionStr),1)=','
    		BEGIN
    			SET @idrversionStr=SUBSTRING(@idrversionStr,1,LEN(@idrversionStr)-1)
    		END
    		
    		DECLARE @xml XML
    		SET @xml='<tb><nod><id>'+
    			    REPLACE(REPLACE(@idrversionStr,',','</rv></nod><nod><id>'),':','</id><rv>')+
    		         '</rv></nod></tb>'	
    	    INSERT  @table
    		SELECT 
    		T.c.value('id[1]','nvarchar(64)'),
    		T.c.value('rv[1]','bigint')
    		FROM @xml.nodes('/tb/nod') AS T(c)	  
    	END
    	RETURN
    END
    
    
    GO
    
    
    

    4.fnGetSplitTwoStr

    分割“aa:AA,bb:BB,cc:CC,dd:DD”字符串为表

    CREATE FUNCTION [dbo].[fnGetSplitTwoStr]
    (	
    	@Str NVARCHAR(MAX)
    )
    RETURNS @table TABLE(str1 NVARCHAR(64),str2 NVARCHAR(64))
    BEGIN
    	IF LEN(@Str)>=3
    	BEGIN
    		SET @Str=RTRIM(LTRIM(@Str));
    		IF SUBSTRING(@Str,1,1)=','
    		BEGIN
    			SET @Str=SUBSTRING(@Str,1,LEN(@Str)-1)
    		END
    		IF SUBSTRING(@Str,LEN(@Str),1)=','
    		BEGIN
    			SET @Str=SUBSTRING(@Str,1,LEN(@Str)-1)
    		END
    		
    		DECLARE @xml XML
    		SET @xml='<tb><nod><id>'+
    			    REPLACE(REPLACE(@Str,',','</rv></nod><nod><id>'),':','</id><rv>')+
    		         '</rv></nod></tb>'	
    	    INSERT  @table
    		SELECT 
    		T.c.value('id[1]','nvarchar(64)'),
    		T.c.value('rv[1]','nvarchar(64)')
    		FROM @xml.nodes('/tb/nod') AS T(c)	  
    	END
    	RETURN
    END
    
    
    
    
    GO
    
    
    

    5.fnGetInfoByUserId

    根据传入的ID得到权限表或者相关操作表

    CREATE FUNCTION [dbo].[fnGetInfoByUserId]
    (	
    	@userID INT
    )
    RETURNS TABLE 
    AS
    RETURN 
    (
    	SELECT A.* FROM Table A
    	INNER JOIN Table B ON A.ID=B.ID
    	WHERE a.UserID=@userID
    )

    6.删除过程,不要用in 效率低

    CREATE PROCEDURE [dbo].[prDel]
    	@ids NVARCHAR(MAX),
    	@userId INT,
    	@Ret INT=0 OUTPUT
    AS
    BEGIN	
    	UPDATE Table
    	SET IsValid = 0
    	FROM Table A
    	INNER JOIN YC.fnGetSplitIdAndRversion(@ids) B ON A.ID=B.id AND 
        A.Rversion=CONVERT(TIMESTAMP,B.rversion)	
    
    END
    


  • 相关阅读:
    http学习笔记(一)
    关于花瓣网header条的思考
    前端知识体系之入门篇总结(一)
    浮动理解【转】
    CSS选择器比较:queryselector queryselectorall
    javascript模块化
    【C#】 Stopwatch详解
    ArcGIS图层添加字段出现:“定义了过多字段”
    [SWMM]出现问题及解决
    ArcGIS:从DEM数据提取对应点的高程
  • 原文地址:https://www.cnblogs.com/songjuntao/p/15145521.html
Copyright © 2020-2023  润新知