当前位置:首页 » 编程语言 » sql变量存储
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

sql变量存储

发布时间: 2023-02-12 00:08:41

㈠ SQL中一条查询语句,得到一列多个结果,现在想用一个变量存储起来,并用;分开,请问如何处理呢

--先将表倒入临时表,方便自已加工,这里加了一个 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