sql语句如何将select出的多个结果insert到一个字段中

sql语句如何将select出的多个结果insert到一个字段中
具体在说就是 将符合一定条件的select结果集插入到另一个表中的指定的一条记录中的一个字段里。

假设表goods(goodsid,shortname)
declare @shortname varchar(50)
declare @name varchar(255) -----需要得到的结果
set @name='' -------初始化,很重要
declare cursor1 cursor for --定义游标cursor1
select top 3 shortname from goods --使用游标的对象(跟据需要填入select文),即你说的Select结果集
open cursor1 --打开游标

fetch next from cursor1 into @shortname --将游标向下移1行,获取的数据放入之前定义的变量@shortname中

while @@fetch_status=0 --判断是否成功获取数据
begin
set @name=@name+@shortname --进行相应处理(跟据需要填入SQL文),我这里根据你的需要叠加。
fetch next from cursor1 into @shortname --将游标向下移1行
end

close cursor1 --关闭游标
deallocate cursor1

最后 @shortname就是你需要得到的结果 然后你插入或者更新你的那个表的字段即可!
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-03-08
insert into 表1 (字段) select [字段A] from [表2] where [ID] = 1 UNION select [字段B] from [表2] where [ID] = 1 UNION select [字段C] from [表2] where [ID] = 1
第2个回答  2010-03-08
不明白你意思的是什么?
如果是把一条记录里面的多个字段合并成一个值
设表tableA字段a,b,c;表tableB,字段D
tableA
a b c
i hate you
想这样插入tableB表
tableB:
d
i hate you
那就这样:
insert into tableB(D)select a||b||c from tableA

如果是把多条记录合成一条的话
得用存储过程了
第3个回答  2010-03-08
你说的是这个意思吧?假设“另一个表”是A,要插入的字段是A.A,则:
UPDATE A
SET A.A=(SELECT a|b|c from .... where ....)
where A...........
第4个回答  2010-03-08
insert into table values (select * FROM table2);
相似回答