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

例如:有两个数据库表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
;
温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-02-11
如果是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
;
第2个回答  2008-10-11
主要是需要注意一下,B表的字段列数是变化的,是需要根据A表的查询结果来生成,试试动态语句,^_^

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网