SQL中一条查询语句,得到一列多个结果,现在想用一个变量存储起来,并用;分开,请问如何处理呢?
selectemailfromxxwhereid=‘1’结果邮箱地址邮箱地址想让变量@email变为邮箱地址;邮箱地址...
select email from xx where id=‘1’ 结果
邮箱地址
邮箱地址 想让变量@email变为 邮箱地址;邮箱地址 展开
邮箱地址
邮箱地址 想让变量@email变为 邮箱地址;邮箱地址 展开
展开全部
--先将表倒入临时表,方便自已加工,这里加了一个 num 字段来记录状态
select email,1 num into #email from where id=1
--定义一些变量,@email是你需要的,@tmpemail 用来临时存储判断条件的
declare @i int,@n int,@email varhcar(8000),@tmpemail varchar(100)
set @email=''
set @i=1
select @n=count(*) from #email
--使用循环获取email,赋值给@email
while @i<=@n
begin
if @email='' then
begin
select top 1 @email=email,@tmpemail=email from #email where num=1 order by email
end
else
begin
select top 1 @email=@email+';'+email,@tmpemail=email from #email where num=1 order by email
end
--将已获取过的记录做标记,将num字段改为0
update #email set num=0 where num=1 and email=@tmpemail
set @i=@i+1
end
--输出变量@email
select @email
drop table #email
select email,1 num into #email from where id=1
--定义一些变量,@email是你需要的,@tmpemail 用来临时存储判断条件的
declare @i int,@n int,@email varhcar(8000),@tmpemail varchar(100)
set @email=''
set @i=1
select @n=count(*) from #email
--使用循环获取email,赋值给@email
while @i<=@n
begin
if @email='' then
begin
select top 1 @email=email,@tmpemail=email from #email where num=1 order by email
end
else
begin
select top 1 @email=@email+';'+email,@tmpemail=email from #email where num=1 order by email
end
--将已获取过的记录做标记,将num字段改为0
update #email set num=0 where num=1 and email=@tmpemail
set @i=@i+1
end
--输出变量@email
select @email
drop table #email
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询