sql 2005 字符函数实例与应用实例
sql 2005 字符函数实例与应用实例
use demo
go
/*
将表code的列string中的值提取放到record表中
string 中字符类型为
dsddddd,2222222,222221,3
其中最后一位为标记对于record表中的biaoji
前面的以','分割的是值对应record表中value
*/
go
drop proc proc_split_code
go
create proc proc_split_code
as
begin
set nocount on
declare @count int --条数
declare @index int --变量
set @index = 1 --默认
select @count = count(*) from code
--print @count
while (@index<=@count)
begin
declare @biaoji int -- 标记
declare @string nvarchar(1000)--字符串
declare @temp int --分隔符的位置
declare @star int --开始位置
declare @code nvarchar(100) --
set @star = 0
select @string=reverse(string)
from (
select row_number() over(order by string) as rownumber,* from code
) as a
where rownumber between @index and @index
set @temp=charindex(',',@string,@star)
set @biaoji = substring(@string,@star,@temp)
print @biaoji
set @string = reverse(@string)
set @temp=charindex(',',@string,@star)
set @star = 0
while(@temp>0)
begin
set @temp=charindex(',',@string,@star)
--print @star
--print @temp
if @temp >0
begin
set @code=substring(@string,@star,@temp-@star)
print @code
--插入到相应的表中
insert into record(biaoji,value,time)
values (@biaoji,@code,getdate())
end
set @star=@temp+1
end
--print @index
print @string
set @index = @index+1
end
end
go
exec proc_split_code
补充:数据库,Mssql