Plsql 使用游标中的别名进行解码
我想用别名在游标中使用decode函数来避免列的歧义,所以我使用了下面的方法 我有如下代码:Plsql 使用游标中的别名进行解码,plsql,Plsql,我想用别名在游标中使用decode函数来避免列的歧义,所以我使用了下面的方法 我有如下代码: declare cl number; cursor c is select c1.rowid,c1.col1, DECODE(c1.col2, 'XYZ', c1.col3, 10) cl from table1 d,table2 c1 where c1.process_id=13525 and d.col3(+
declare
cl number;
cursor c is
select c1.rowid,c1.col1,
DECODE(c1.col2, 'XYZ', c1.col3, 10) cl
from table1 d,table2 c1 where c1.process_id=13525 and d.col3(+)=cl;
begin
for rec in c
loop
dbms_output.put_line(NVL(rec.cl,'-1'));
end loop;
end;
在这种情况下,当我通过删除条件“和d.col3(+)=cl”来启动查询时,它将检索值为“cl”的数据。但当我指定这个条件时,它不会检索数据,也不会进入游标的循环
假设我得到的cl为5,那么它也存在于d.col3中,那么它应该给我数据,我这样做是因为我需要删除重复的记录。因为在这个条件下,我会得到重复的记录。这里,d表中的col3是主键
所以我不明白为什么它不会进入循环,因为它从查询中获取值。您不能在
WHERE
子句中使用别名:
在这种情况下,子查询或CTE可能会有所帮助。诸如此类(未经测试!):
在得到使用WITH子句的建议后,我使用替代表名通过DECODE()检索数据的方法是:
declare
cl number;
cursor c is
with V as (
select c1.process_id,
DECODE(c1.col2, 'BANDM', c1.col3, 10) cl
from table2 c1)
select c1.rowid rid,c1.col1, V.cl from table1 d,V,table2 c1
where V.process_id=1
and d.col3(+)=V.cl
and c1.col3=V.cl;
begin
for rec in c
loop
dbms_output.put_line(NVL(rec.rid,'-1'));
dbms_output.put_line(NVL(rec.cl,'-1'));
end loop;
end;
没有WITH条款的另一个解决方案是:
declare
c2 number;
cursor c is
select c1.process_id
c1.rowid,
c1.col1,
DECODE(c1.col2, 'BANDM', c1.col3, 10) as c2
from table1 d,
table2 c1
where c1.process_id=1
and d.col3(+) = DECODE(c1.col2, 'BANDM', c1.col3, 10);
begin
for rec in c
loop
dbms_output.put_line(NVL(rec.rid,'-1'));
dbms_output.put_line(NVL(rec.c2,'-1'));
end loop;
end;
不,没有任何错误消息。它不会进入循环,因为它不会检索任何数据。的可能重复
declare
c2 number;
cursor c is
select c1.process_id
c1.rowid,
c1.col1,
DECODE(c1.col2, 'BANDM', c1.col3, 10) as c2
from table1 d,
table2 c1
where c1.process_id=1
and d.col3(+) = DECODE(c1.col2, 'BANDM', c1.col3, 10);
begin
for rec in c
loop
dbms_output.put_line(NVL(rec.rid,'-1'));
dbms_output.put_line(NVL(rec.c2,'-1'));
end loop;
end;