oracle动态表名查询 如何写sql语句或者存储过程实现: 根据某张表中的某个字段的值决定查询哪一张表

2025-05-09 07:46:15
推荐回答(4个)
回答1:

begin
for cur in(select id,flag fromu a)
loop
if cur.flag=0 then
select * from b;
...
else
select * from C;
...
end if;
end loop;
end;

回答2:

用动态语句
或试试这样行不行
select * from decode(select flag from a where id=1,0,(select * from b),1,(select * from c))

回答3:

declare
as_flag varchar2(10);
as_sql varchar2(300);
begin
select FLAG into as_flag from A where ID = 1;
if as_flag = '0' then
select * from b where ....;
else
select * from C where ....;
end if;

end;

回答4:

declare
as_flag varchar2(10);
as_sql varchar2(300);
begin
select FLAG into as_flag from A where ID = 1;
if as_flag = '0' then
insert into 结果表(xx..xx) select xx..xx from b where ....;
else
insert into 结果表(xx..xx) select xx..xx from c where ....;
end if;

end;