如何为游标返回的每一行输出动态SQL的结果?
这是我的发言。我想使用动态SQL和dblinks比较表。
DECLARE
CURSOR c1 IS
select dbname, dblink from monitor.dblink;
v_dbname VARCHAR2(30);
v_dblink VARCHAR2(30);
v_sql VARCHAR2(200);
v_value VARCHAR2(30);
BEGIN
OPEN c1;
LOOP
FETCH c1 INTO v_dbname,v_dblink;
EXIT WHEN c1%NOTFOUND;
BEGIN
v_sql:= 'select concat(owner,table_name) from monitor.exp_tab_mon where dbname='||q'('v_dbname')'||' minus select concat(owner,table_name) from dba_tables@'||v_dblink;
execute immediate v_sql into v_value;
EXCEPTION
when no_data_found then
dbms_output.put_line('RETURN' || v_value);
END;
dbms_output.put_line('RETURN' || v_value);
END LOOP;
END;
/
输出是:
SQL> /
RETURN
RETURN
RETURN
RETURN
RETURN
RETURN
RETURN
RETURN
RETURN
RETURN
RETURN
RETURN
RETURN
RETURN
RETURN
如何更改此项,以便在“找不到数据”输出为0时,输出数据。
答案 0 :(得分:1)
您可以隐藏no_data_found异常,并仅通过v_value显示所需的输出:
Bharath 01061992
Ragul 10302002
Bala 01171993
Arjun 05142003
Vimal 06301997
Ramesh 05171997
Kamal 05151997
Vinoth 05201997
答案 1 :(得分:0)
答案 2 :(得分:0)
我相信每次找不到数据时,你会得到两行输出。尝试使用我在异常块中添加的continue;
,以便将所需的输出放在异常场景中,然后继续循环。
DECLARE
CURSOR c1 IS
select dbname, dblink from monitor.dblink;
v_dbname VARCHAR2(30);
v_dblink VARCHAR2(30);
v_sql VARCHAR2(200);
v_value VARCHAR2(30);
BEGIN
OPEN c1;
LOOP
FETCH c1 INTO v_dbname,v_dblink;
EXIT WHEN c1%NOTFOUND;
BEGIN
v_sql:= 'select concat(owner,table_name) from monitor.exp_tab_mon where dbname='||q'('v_dbname')'||' minus select concat(owner,table_name) from dba_tables@'||v_dblink;
execute immediate v_sql into v_value;
EXCEPTION
when no_data_found then
dbms_output.put_line('0' || v_value);
continue;
END;
dbms_output.put_line('RETURN' || v_value);
END LOOP;
END;
/