分割字符串的问题

purexiafeng 2007-08-10 02:46:59
如:@str ='1,2,3,4,5,9'分割出来
然后插到一张表中
表结构如下:
CREATE TABLE IDS
(
IDS INT,
IDNAME VARCHAR(50)
)

将上面的写到一个存储过程中。
请大家帮帮忙!
...全文
105 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
子陌红尘 2007-08-10
  • 打赏
  • 举报
回复
CREATE TABLE IDS
(
IDS INT,
IDNAME VARCHAR(50)
)
go

create procedure sp_test(@str varchar(8000))
as
begin
while charindex(',',@str)>0
begin
insert into IDS(IDS) select left(@str,charindex(',',@str)-1)
set @str=stuff(@str,1,charindex(',',@str),'')
end
insert into IDS(IDS) select @str
end
go

exec sp_test '1,2,3,4,5,9'

select * from IDS

/*
IDS IDNAME
----------- --------------------------------------------------
1 NULL
2 NULL
3 NULL
4 NULL
5 NULL
9 NULL
*/
go

drop procedure sp_test
drop table IDS
go
子陌红尘 2007-08-10
  • 打赏
  • 举报
回复
CREATE TABLE IDS
(
IDS INT,
IDNAME VARCHAR(50)
)
go

create procedure sp_test(@str varchar(8000))
as
begin
while charindex(',',@str)>0
begin
insert into IDS(IDS) select left(@str,charindex(',',@str)-1)
set @str=stuff(@str,1,charindex(',',@str),'')
end
insert into IDS(IDS) select @str
end
go

exec sp_test '1,2,3,4,5,9'

select * from IDS
go

drop procedure sp_test
drop table IDS
go
purexiafeng 2007-08-10
  • 打赏
  • 举报
回复
数字都放到IDS字段中!

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧