zoukankan      html  css  js  c++  java
  • 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
    


  • 相关阅读:
    什么是“开放构造类型”,“封闭构造类型”
    代码整洁整洁之道读书笔记 2
    学习 WCF By Visual Studio 2010 (1)起步“全双工”
    代码整洁整洁之道读书笔记 1
    学习 WCF By Visual Studio 2010 (2) 宿主
    持久层通用组件设计与范例,写给某孩子(基于JDBC)
    [zz]JS后退一页, JS返回上一页代码, JS返回下一页,JS返回第几页,JS页面跳转, JS引用JS
    web下vs2008+crystal report 不预览直接打印
    用SQL语句删除重复记录
    Gridview 添加行.
  • 原文地址:https://www.cnblogs.com/songjuntao/p/15145521.html
Copyright © 2011-2022 走看看