34,594
社区成员
发帖
与我相关
我的任务
分享
if object_id('dbo.fn_split')is not null drop function dbo.fn_split
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
if object_id('tb')is not null drop table tb
go
create table tb(ID int, BillDetail_ID int, GoodRecord_ID int, BuildDate datetime)
--调用
declare @s varchar(1000)
set @s='1,2,3,4,5,6,7,8,9'
insert tb select 1,1,a,getdate() from dbo.fn_split(@s,',')
select * from tb
/*
(影響 9 個資料列)
ID BillDetail_ID GoodRecord_ID BuildDate
----------- ------------- ------------- ------------------------------------------------------
1 1 1 2008-12-25 09:47:11.930
1 1 2 2008-12-25 09:47:11.930
1 1 3 2008-12-25 09:47:11.930
1 1 4 2008-12-25 09:47:11.930
1 1 5 2008-12-25 09:47:11.930
1 1 6 2008-12-25 09:47:11.930
1 1 7 2008-12-25 09:47:11.930
1 1 8 2008-12-25 09:47:11.930
1 1 9 2008-12-25 09:47:11.930
(影響 9 個資料列)
*/
/*
功能:实现split功能的函数
*/
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='1,2,3,4,5,6,7,8,55'
select ID = 1 , * , BuildDate = getdate() from dbo.fn_split(@s,',')
drop function dbo.fn_split
/*
ID a BuildDate
----------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------
1 1 2008-12-25 09:44:17.047
1 2 2008-12-25 09:44:17.047
1 3 2008-12-25 09:44:17.047
1 4 2008-12-25 09:44:17.047
1 5 2008-12-25 09:44:17.047
1 6 2008-12-25 09:44:17.047
1 7 2008-12-25 09:44:17.047
1 8 2008-12-25 09:44:17.047
1 55 2008-12-25 09:44:17.047
(所影响的行数为 9 行)
*/
那就用子符串分解函数来实现.
/*
功能:实现split功能的函数
*/
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='1,2,3,4,5,6,7,8,55'
select * from dbo.fn_split(@s,',')
drop function dbo.fn_split
--如果是自动生成一组连续的数值
--生成一个临时表
SELECT TOP 10000 id = identity(int,1,1) INTO # FROM syscolumns a, syscolumns b
insert into TAB select id , id , id , getdate() from #
---分割函數
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='1,2,3,4,5,6,7,8,55'
select * from dbo.fn_split(@s,',')
drop function dbo.fn_split
insert TAB values( 1,(1,2,3..... 有上W条记录),getdate())
----可以改成下面的试试:
insert TAB select 1, 1, getdate()
union all select 2, 2, getdate()
--...
union all select 10000,1,getdate()