SQL Server splits the string to a temporary table without GOCreatefunction [dbo]. [split] (@ SourceSqlvarchar (max), @ StrSepratevarchar (10) returns @ temptable (linevarchar (max) asbegindeclare @ iintset @ SourceSqlrtrim (ltrim (@ SourceSql )) set @ icharindex (@ StrSep
SQL Server Splits a string to a temporary table without the GOCreate function [dbo]. [split] (@ SourceSql varchar (max), @ StrSeprate varchar (10) returns @ temp table (line varchar (max )) asbegin declare @ I int set @ SourceSql = rtrim (ltrim (@ SourceSql) set @ I = charindex (@ StrSep
SQL Server splits strings into temporary tables <无>
GOCreate function [dbo]. [split] (@ SourceSql varchar (max), @ StrSeprate varchar (10) returns @ temp table (line varchar (max )) asbegin declare @ I int set @ SourceSql = rtrim (ltrim (@ SourceSql) set @ I = charindex (@ StrSeprate, @ SourceSql) while @ I> = 1 begin if len (left (@ SourceSql, @ i-1)> 0 begin insert @ temp values (left (@ SourceSql, @ i-1 )) end set @ SourceSql = substring (@ SourceSql, @ I + len (@ StrSeprate), len (@ SourceSql)-@ I) set @ I = charindex (@ StrSeprate, @ SourceSql) end if @ SourceSql <> ''insert @ temp values (@ SourceSql) returnend ----- test GOSelect * From dbo. split ('asd, f, gh, jk, L ',',')
-- SQL recursive Create function [dbo]. [split] (@ SourceSql varchar (max), @ StrSeprate varchar (10) returns @ temp table (line varchar (max) asbegin; with roy as (select COl2 = cast (substring (@ SourceSql + @ StrSeprate, 1, charindex (@ StrSeprate, @ SourceSql + @ StrSeprate)-1) as nvarchar (max )), split = cast (substring (@ SourceSql + @ StrSeprate, charindex (@ StrSeprate, @ SourceSql + @ StrSeprate) + 1, len (@ SourceSql + @ StrSeprate)-charindex (@ StrSeprate, @ SourceSql + @ StrSeprate) as varchar (max) union allselect COl2 = cast (substring (Split, 1, charindex (@ StrSeprate, Split)-1) as nvarchar (100) as nvarchar (max), Split = cast (substring (Split, charindex (@ StrSeprate, Split) + 1, len (Split) -charindex (@ StrSeprate, Split) as varchar (max) from Roy where charindex (@ StrSeprate, Split)> 1) Insert into @ temp (line) select COl2 from roy option (MAXRECURSION 0) returnend