--字符串根据特定字符分割成表数据
create function [dbo].[f_split](@SourceSql varchar(8000),@StrSeprate varchar(10))
returns @temp table(a varchar(100))
as
begin
declare @i int
set @SourceSql=rtrim(ltrim(@SourceSql))
set @i=charindex(@StrSeprate,@SourceSql)
while @i>=1
begin
insert @temp values(left(@SourceSql,@i-1))
set @SourceSql=substring(@SourceSql,@i+1,len(@SourceSql)-@i)
set @i=charindex(@StrSeprate,@SourceSql)
end
if @SourceSql<>‘\‘
insert @temp values(@SourceSql)
return
end
把查出来的表一个单列转换成字符串用,隔开。
create FUNCTION [dbo].[f_GetUserIdStr]
(
@userId dbo.UserIdInfo READONLY
)
RETURNS NVARCHAR(4000)
AS
BEGIN
DECLARE @uidStr NVARCHAR(4000)
SET @uidStr =STUFF((SELECT ‘,‘+CAST(UserId AS NVARCHAR(1024)) FROM @userId for xml path(‘‘)),1,1,‘‘)
RETURN @uidStr
END