请教如何将以下的字串资料转成如表a
111,222,333,444,555
表a:
111
222
333
444
555
111,222,333,444,555
表a:
111
222
333
444
555
解决方案 »
- 菜鸟请教一个SQL写法
- 表的主键(user_id)为not null,如何写插入语句
- 用户完整性设置
- 对于nvarchar的列,怎样筛选出大于0的值?
- 如何弥补insert into 查询语句中不能order 的缺陷
- 运行时SQL语句出现除零错误,怎么办?
- 求一个SQL语句
- 为什么从sql server 取来的中文数据在asp页面上显示(在win98上)乱码,有些字是正常的,已经用了charset=gb2312
- 请求帮助>>>>>>>>>
- 数据库从服务器中分离后,没附加回去,我就把服务器删了。重新注册后,登陆不上去了(急,等)
- 求字符相加带'/'的SQL语句
- 求一SQL语句,关于分组显示的,谢谢!
go
declare @sql varchar(2000)
declare @str varchar(200)
set @str='111,222,333,444,555'
set @str=replace(@str,',',' union select ')
set @sql='insert into #t select '+@str
print @sql
exec(@sql)
select * from #t
drop table #t
-----------------------
str
-----------------------
111
222
333
------------------------
insert into #t select 111 union select 222 union select 333 union select 444 union select 555(5 行受影响)(5 行受影响)
444
555
drop table a
create table a(id varchar(50))create proc Pcomma
@s varchar(50)
as
declare @idex int,@s1 varchar(50)
set @s1=''
while(charindex(',',@s)!=0)
begin
set @idex=charindex(',',@s)
select @s1=substring(@s,1,@idex-1)
set @s=substring(@s,@idex+1,len(@s))
insert into a
select @s1
end
set @s1=@s
insert into a
select @s1exec Pcomma '1,23,56,44,tt'
insert @a
select '111'
union all
select '222'
union all
select '333'
union all
select '444'declare @name varchar(80)
set @name=''
select @name=@name+','+name from @a
set @name=stuff(@name,1,1,'')
print @name/*(所影响的行数为 4 行)111,222,333,444
*/
若您的方法使用函数中,可行吗?有需要修改吗?谢谢
在函数中的'EXECUTE STRING' 使用副作用或时间相依运算子无效。
set @str='111,222,333,444'declare @n int
set @n=0
set @str=@str+','declare @a table(name varchar(80))
while(@n<len(@str)/4)
begin
insert @a
select substring(@str,4*@n+1,3)
set @n=@n+1
end
select * from @a/*
name
--------------------------------------------------------------------------------
111
222
333
444(所影响的行数为 4 行)
*/