sql语句写一个存储过程 将 三张表中的各一个字段数据提取插入一张新表中,但要求有两个字段是互斥的

有一出现,另一个的那一列 就显示空白
create procedure add_table
is
begin
insert into wip_led_opration(operation_id,led_pc_id,led_screen_id)
select operation_id,led_pc_id,led_screen_id from fnd_led_pc ,fnd_led_screen ,fnd_operation
where fnd_operation.status=1 and fnd_led_screen.status=1 and fnd_led_pc.status=1 and fnd_operation.status=fnd_led_screen.status and fnd_led_screen.status=fnd_led_pc.status ;
end;

即pc_id,screen_id只能出现一个

pc_id,screen_id哪个优先?以pc_id优先为例:
oracle用decode函数,sqlserver可以用case...when,给你个oracle的例子
create procedure add_table
is
begin
insert into wip_led_opration(operation_id,led_pc_id,led_screen_id)
select operation_id,led_pc_id, decode(led_pc_id,null,led_screen_id,null)
-- 逻辑:第三列led_screen_id,先判断led_pc_id是否为空,为空则用led_screen_id,非空即led_pc_id存在,则led_screen_id列位置留空
from fnd_led_pc ,fnd_led_screen ,fnd_operation
where fnd_operation.status=1
and fnd_led_screen.status=1
and fnd_led_pc.status=1
-- and fnd_operation.status=fnd_led_screen.status --这个条件是多余的,两个值都=1了
and fnd_led_screen.status=fnd_led_pc.status ;
end;
/* 另外提几点建议
1. 建议给三个表加上别名
2. 多余的连接条件我注释掉了,虽然对执行计划应该没有太大影响
3. SELECT后最好标识出源表,比如fnd_operation.operation_id,这样自己看着也清楚
*/
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-12-23
pc_id,screen_id同时出现去掉哪个?

下例假设去掉screen_id
select
operation_id,
led_pc_id,
(case when led_pc_id is not null then null else led_screen_id end) led_screen_id
...追问

另外两张表里 都有这两个字段的数据,

要在新建的那张表中实现效果 即一条生产线要么只能生产 液晶屏 要么生产液晶电脑

两个都要输出但是 输出pc_id时screen_id那一列为空(就是不insert)
输出screen_id时pc_id那一列为空(就是不insert)

追答

你的意思不大明白
我上面sql文的意思就是pc_id不为空时screen_id为空(即,插入NULL)

如果你需要的是两列并一列的话
insert into wip_led_opration(operation_id,led_id)
select
operation_id,
(case when led_pc_id is not null then led_pc_id else led_screen_id end) led_id
...

本回答被网友采纳