sql数组插入

alter proc insertup
(@unam nchar(20),@ups nchar(30))
as
begin
insert into usertb(uname) values(@unam)
declare @dou int
declare @coudou int
declare @str int
declare @id int
set @id=@@identity
set @coudou=1

print @coudou


while(charindex(',',@ups,@coudou)>0)
begin
set @dou=charindex(',',@ups,@coudou)
print @id
set @str=cast(substring(@ups,@dou-1,1) as int)
print @str
insert into up(uid,pid) values(@id, @str )
set @coudou=@dou+1

end
if(charindex(',',@ups,@coudou)=0)
begin
set @str=cast(substring(@ups,len(@ups),1) as int)
print @str
insert into up(uid,pid) values(@id, @str )
end
end

说白了也就是charindex和substring函数的使用。这只是一种方法,在代码里写就更简单了

你可能感兴趣的:(sql)