怎么写啊?我SQL基础很差的 麻烦给写下吧 谢了
解决方案 »
- oracle varchar(?) 很棘手的一个问题!!求大神帮助!!
- PL/SQL中在package里定义的常量与在package body中的常量有什么区别?
- 求一个SQL
- 怎样用SQL语句指定使用哪个方案?
- 同样版本的ORACLE 数据库 为什么具体执行的时候会有所差异
- oracle9i可以安装在xp Professional版本什么吗?应该注意些什么?
- 快看快看快看,我新学oracle,请帮我
- 请问高手:大数据量情况下如何返回指定条数的记录?
- ORA-03113: 通信通道的文件结尾和alert里的 ORA-07445: 出现异常错误
- Oracle 在pl/sql develop查询数据,不加sid前缀查询不出来数据是什么原因?
- 通过数据库链接远程调用存储过程的问题
- ora-03113~~~在线=,急
with tableA as
(
select '201309' ym,'张三' name,'01' nameid,'济南' adress from dual union all
select '201309' ym,'丢丢' name,'02' nameid,'北京' adress from dual
),tableB as
(
select '201308' ym,'李四' name,'01' nameid,'南京' adress from dual union all
select '201308' ym,'豆豆' name,'02' nameid,'北京' adress from dual
)select ym,nameid,'A' tbCode,'name' fdCode,n1 last,n2 end
from (
select a.nameid,a.ym,a.name n1,b.name n2
from tableA a left join tableB b on a.nameid = b.nameid
where a.name<>b.name )
union all
select ym,nameid,'A' tbCode,'adress' fdCode,a1 last,a2 end
from (
select a.nameid,a.ym,a.adress a1,b.adress a2
from tableA a left join tableB b on a.nameid = b.nameid
where a.adress<>b.adress) ym nameid tbCode fdCode last end
-----------------------------------------------------------
1 201309 01 A name 张三 李四
2 201309 02 A name 丢丢 豆豆
3 201309 01 A adress 济南 南京
对的 nameId 相同 就计入 C表
那就按照上面写的插入到表C就可以咯
insert into tableCselect ym,nameid,tbCode,fdCode,last,end
from
(
select ym,nameid,'A' tbCode,'name' fdCode,n1 last,n2 end
from (
select a.nameid,a.ym,a.name n1,b.name n2
from tableA a left join tableB b on a.nameid = b.nameid
where a.name<>b.name )
union all
select ym,nameid,'A' tbCode,'adress' fdCode,a1 last,a2 end
from (
select a.nameid,a.ym,a.adress a1,b.adress a2
from tableA a left join tableB b on a.nameid = b.nameid
where a.adress<>b.adress)
)
create table B(ym int, name varchar2(12), nameId varchar2(10), address varchar2(100));
create table C(
ym int, nameId varchar2(10), tbCode varchar2(10),
fdCode varchar2(15), last varchar2(100), end varchar2(100)
);
insert into A(ym, name, nameId, address) values(201309, '张三', '01', '济南');
insert into B(ym, name, nameId, address) values(201309, '李四', '01', '泰安');declare
cursor c1 is
select a.*, b.name b_name, b.address b_address
from A, B
where a.nameId=b.nameId and a.ym=b.ym;
v_sql varchar2(1024);
v_sql_pre varchar2(100);
begin
for c1_res in c1 loop
v_sql_pre := 'insert into c(ym, nameId, tbCode, fdCode, last, end) values('||c1_res.ym||', '''||c1_res.nameId||''', ''A'', ';
if c1_res.name != c1_res.b_name then
v_sql := v_sql_pre||'''name'', '''||c1_res.name||''', '''||c1_res.b_name||''')';
execute immediate v_sql;
end if;
if c1_res.address != c1_res.b_address then
v_sql := v_sql_pre||'''address'', '''||c1_res.address||''', '''||c1_res.b_address||''')';
execute immediate v_sql;
end if;
end loop;
end;
/select * from c;
那就按照上面写的插入到表C就可以咯
insert into tableCselect ym,nameid,tbCode,fdCode,last,end
from
(
select ym,nameid,'A' tbCode,'name' fdCode,n1 last,n2 end
from (
select a.nameid,a.ym,a.name n1,b.name n2
from tableA a left join tableB b on a.nameid = b.nameid
where a.name<>b.name )
union all
select ym,nameid,'A' tbCode,'adress' fdCode,a1 last,a2 end
from (
select a.nameid,a.ym,a.adress a1,b.adress a2
from tableA a left join tableB b on a.nameid = b.nameid
where a.adress<>b.adress)
)
我要是一个表有三四十个字段那不要对比三四十次啊 ! SQL太大了吧?唉!!