现在有N个字符串,比如
111
222
333
444
555想插入一个临时表#AAA中最笨的方法
CREATE TABLE #AAAINSERT INTO #AAA VALUES (111)
INSERT INTO #AAA VALUES (222)
INSERT INTO #AAA VALUES (333)
INSERT INTO #AAA VALUES (444)
INSERT INTO #AAA VALUES (555)有没有什么好方法呀比如类似SELECT 111, 222, 333, 444, 555
INTO #AAA
111
222
333
444
555想插入一个临时表#AAA中最笨的方法
CREATE TABLE #AAAINSERT INTO #AAA VALUES (111)
INSERT INTO #AAA VALUES (222)
INSERT INTO #AAA VALUES (333)
INSERT INTO #AAA VALUES (444)
INSERT INTO #AAA VALUES (555)有没有什么好方法呀比如类似SELECT 111, 222, 333, 444, 555
INTO #AAA
union all select 222
union all select 333
union all select 444
union all select 555--or:
create table #AAA(col int)insert #AAA select 111
union all select 222
union all select 333
union all select 444
union all select 555
go
create function dbo.fn_split
(
@inputstr varchar(8000),
@seprator varchar(10)
)
returns @temp table (a varchar(200))
as
begin
declare @i int
set @inputstr = rtrim(ltrim(@inputstr))
set @i = charindex(@seprator, @inputstr)
while @i >= 1
begin
insert @temp values(left(@inputstr, @i - 1))
set @inputstr = substring(@inputstr, @i + 1, len(@inputstr) - @i)
set @i = charindex(@seprator, @inputstr)
end
if @inputstr <> '\'
insert @temp values(@inputstr)
return
end
go
--调用
declare @s varchar(1000)
set @s='111, 222, 333, 444, 555'
select * into #AAA from dbo.fn_split(@s,',')
select * from #AAA
drop table #AAA
/*a
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
111
222
333
444
555(影響 5 個資料列)
*/