用sql语句查询结果插入到新的数据库中

来源:百度知道 编辑:UC知道 时间:2024/05/25 04:41:51
例如:有两个数据库表A和B
A:字段:类别,内容;其对应值为:
101,a
101,b
101,c
102,d
102,e
查询相同类别的内空到B库对应的类别和内容字段中,插入B后的什为:
101,a,b,c
102, d,e
请高手指点如何实现?
sql server 2000数据库

如果是oracle,用存储过程可以解决这个问题:
create procedure p_1 is
v_old_id number(5); --假设类别是数值型
v_tmp varchar2(40);
begin
setnull(v_old_id);
for c1 in(selct * from 表a order by 类别)
loop
if v_old_id is null or v_old_id<>c1.类别 then
v_old_id=c1.类别;
if v_old_id<>c1.类别 then
insert into 表B
values(v_old_id,substr(v_temp,1,length(v_temp)-1)
;
commit
;
v_temp:=c1.内容||','
;
end if;
else
v_temp:=v_temp||c1.内容||','
;
end if
;
end loop
;
commit
;
end
;

主要是需要注意一下,B表的字段列数是变化的,是需要根据A表的查询结果来生成,试试动态语句,^_^