SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER 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
调用 dbo.f_split(@userIdStr,',')
@userIdStr=1235123,235123623,123341235,234345234,2346424
第二个参数为你所按照某字符分割
最后返回一个表类型 table;
把查出来的表一个单列转换成字符串用,隔开。return=1235123,235123623,123341235,234345234,2346424
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER 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
调用 SET @uisStr=dbo.f_GetUserIdStr(@userIdInfo)
最后返回字符串;
这里的参数是一个自定义的表,dbo.UserIdInfo。
可以自定义一个表,用于变量作为参数。
DROP TYPE [dbo].[UserIdInfo] GO CREATE TYPE [dbo].[UserIdInfo] AS TABLE( [UserId] [BIGINT] NULL, [NickName] [NVARCHAR](100) NULL ) GO
和正常创建表差不多。