sqlserver常用表值函数

Posted SJT

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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


以上是关于sqlserver常用表值函数的主要内容,如果未能解决你的问题,请参考以下文章

sqlserver中的表值函数和标量值函数

SQLServer:为啥要避免使用表值用户定义函数?

sql2005中 表值函数是啥

sql2005中 表值函数是啥

sqlserver 多语句表值函数例子

Sqlserver表值函数来获取逗号分隔的ID