我有另一个线程来解决另一个问题,现在我被困在另一个看似简单的错误上。我的鳕鱼如下:
declare
update_count integer := 0;
prjt_name varchar2(100) not null := '01213264B';
cursor my_cur is (select table_name from all_tab_columns@adhoc_pos15 where column_name = 'PROJECT_ID' and owner = 'SANDBOX');
tableName my_cur%rowtype;
begin
for tableName in my_cur
loop
update_count := 0;
Execute immediate
'select count(t.project_id) as "CNT" from sandbox.'
|| tableName
|| '@adhoc_pos15 t'
|| 'where t.project_id = (select project_id from sandbox.sb_project@adhoc_pos15 where project_name = upper('
|| prjt_name
|| '))'
into update_count;
if update_count = 0 then
execute immediate
'DELETE FROM my_cur where table_name = '
|| tableName;
end if;
end loop;
end;
我的错误讯息是
ORA-06550: line 11, column 8:
PLS-00306: wrong number or types of arguments in call to '||'
ORA-06550: line 10, column 6:
PL/SQL: Statement ignored
ORA-06550: line 20, column 8:
PLS-00306: wrong number or types of arguments in call to '||'
ORA-06550: line 19, column 6:
PL/SQL: Statement ignored
如果您有兴趣。我将通过一个链接到我之前得到的上一个错误。您可以看到代码最初的样子。
编辑1:Per Valli的建议。我更新了我的编码并在第15行得到了错误表达式。
declare
query varchar2(10000);
update_count integer := 0;
prjt_name varchar2(100) := '01213264B';
cursor my_cur is (select table_name from all_tab_columns@db2where column_name = 'PROJECT_ID' and owner = 'SANDBOX');
tableName varchar2(100);
begin
open my_cur;
loop
fetch my_cur into tableName;
exit when my_cur%NOTFOUND;
update_count := 0;
execute immediate
'select count(project_id) as "CNT" from sandbox.' || tableName || '@db2 '
|| ' where project_id = (select project_id from sandbox.sb_project@db2 where project_name = ''' || prjt_name || ''' ) '
into update_count;
if update_count > 0 then
dbms_output.put_line (tableName);
end if;
end loop;
close my_cur;
end;
我错过了一个" ="符号。这现在运行。我得到的结果不是错误信息
ORA-29913: error in executing ODCIEXTTABLEOPEN callout
ORA-29400: data cartridge error
KUP-04040: file ext_qsp_benefit.dat in DATA_DIR not found
ORA-02063: preceding 3 lines from ADHOC_POS15
ORA-06512: at line 13
最终编辑:成功!显然我无法查询某些表格。所以我就把那些桌子拿出来了。
最终编码是:
declare
query varchar2(10000);
update_count integer := 0;
prjt_name varchar2(100) := '01213264B';
cursor my_cur is (select table_name from all_tab_columns@db2 where column_name = 'PROJECT_ID' and owner = 'SANDBOX' and table_name in ('X') );
tableName varchar2(100);
begin
open my_cur;
loop
fetch my_cur into tableName;
exit when my_cur%NOTFOUND;
update_count := 0;
execute immediate
'select count(project_id) as "CNT" from sandbox.' || tableName || '@db2 '
|| ' where project_id = (select project_id from sandbox.sb_project@db2 where project_name = ''' || prjt_name || ''' ) '
into update_count;
if update_count > 0 then
dbms_output.put_line (tableName);
end if;
end loop;
close my_cur;
end;
这并不能完全符合我的要求。它将结果发送到dbms_output。但这是一个开始!谢谢大家的帮助!
答案 0 :(得分:0)
declare
update_count integer := 0;
prjt_name varchar2(100) := '01213264B';
tablename varchar2(100);
cursor my_cur is
select table_name
from all_tab_columns@adhoc_pos15
where column_name = 'PROJECT_ID' and owner = 'SANDBOX';
begin
OPEN my_cur;
LOOP
FETCH my_cur into tableName;
EXIT WHEN my_cur%NOTFOUND;
update_count := 0;
Execute immediate
'select count(t.project_id) as "CNT" from sandbox.'
|| tableName
|| '@adhoc_pos15 t'
|| ' where t.project_id in (select project_id from sandbox.sb_project@adhoc_pos15 where project_name = upper('
|| prjt_name
|| '))'
INTO update_count;
if update_count = 0 then
execute immediate
'DELETE FROM my_cur where table_name = '
|| tablename;
end if;
end loop;
CLOSE my_cur;
end;
你最后错过了连接。一些语法错误
注意: -
为什么这里需要子查询?
where t.project_id in (select project_id from sandbox.sb_project@adhoc_pos15 where project_name = upper('
|| prjt_name
您可以替换为
where t.project_name = prjt_name
答案 1 :(得分:0)
有一件事看起来不对,prjt_name
是一个字符串,但你在查询中将其作为一个数字传递。所以这就是我要改变的地方:
|| 'where t.project_id = (select project_id from sandbox.sb_project@adhoc_pos15 where project_name = upper('''
|| prjt_name
|| '''))'
这基本上会在字符串值周围添加' '
。
然后,由于执行时出现2个错误,因此tableName必定存在问题。我宁愿使用隐式游标,但你应该尝试访问游标的内容,如下所示:
'select count(t.project_id) as "CNT" from sandbox.'
|| tableName.table_name
|| '@adhoc_pos15 t'
|| 'where t.project_id = (select project_id from sandbox.sb_project@adhoc_pos15 where project_name = upper('''
|| prjt_name
|| '''))'
你必须从光标中移除delete
...这看起来完全错误。循环只是移动到下一个记录
希望这有帮助。