2023年7月9日发(作者:)
sql把字符数组转换成表:把字符串1,2,3变成表⾥的⾏数据
需求:把字符串1,2,3变成表⾥的⾏数据⽅法:⽤⾃定义函数实现/* 获取字符串数组的 Table*/if exists (select 1 from sysobjects where id = object_id('Get_StrArrayStrOfTable' )) drop Function Get_StrArrayStrOfTablegoCREATE function Get_StrArrayStrOfTable( @SourceSql varchar (max), @StrSeprate varchar (10))returns @temp table( F1 varchar (100))asbegin 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)
returnendGO
⽤法:SELECT * from _StrArrayStrOfTable('1,2,3',',')
发布者:admin,转转请注明出处:http://www.yc00.com/news/1688907868a182297.html
评论列表(0条)