在sql server2005中分离字符串
1.创建自定义函数
create function [dbo].[StringSplit](@str nvarchar(max),@admin_id bigint,@spliter nvarchar(10))
returns @tb table(ch nvarchar(256))
AS
BEGIN
select @str=admin_duty from admininfo where admin_id=@admin_id
DECLARE @Num int,@Pos int, @NextPos int
SET @Num = 0
SET @Pos = 1
WHILE(@Pos <= LEN(@str))
BEGIN
SELECT @NextPos = CHARINDEX(@spliter, @str, @Pos)
IF (@NextPos = 0 OR @NextPos IS NULL)
SELECT @NextPos = LEN(@str) + 1
INSERT INTO @tb VALUES(RTRIM(LTRIM(SUBSTRING(@str, @Pos, @NextPos - @Pos))))
SELECT @Pos = @NextPos+1
END
return
END
2。在存储过程中调用
ALTER Procedure [dbo].[GetAdminTreeByAdminID]
@AdminID bigint
AS
BEGIN
IF @AdminID=0
BEGIN
SELECT * FROM AdminTreeInfo WHERE [AT_Del_Flag]=0
END
ELSE
BEGIN
SELECT * FROM AdminTreeInfo WHERE AT_ID IN (SELECT * FROM dbo.StringSplit('',@AdminID,',')) AND [AT_Del_Flag]=0
END
END