2017-10-20 125 views
0

我有另一個線程來解決不同的問題,現在我卡在另一個,看似簡單,錯誤。我的鱈魚如下:PL/SQL - 連接時出現此錯誤:PLS-00306:調用'||'時參數的數量或類型錯誤

declare 
    update_count integer := 0; 
    prjt_name varchar2(100) not null := '01213264B'; 
    cursor my_cur is (select table_name from [email protected]_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 [email protected]_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 

如果你有興趣。我會通過一個鏈接指出我以前遇到的錯誤。你可以看到代碼最初的樣子。

Encountered 'Loop' Error

編輯1:每瓦利的建議。我已更新我的編碼,並在第15行獲取錯誤缺失表達。

declare 
    query varchar2(10000); 
    update_count integer := 0; 
    prjt_name varchar2(100) := '01213264B'; 
    cursor my_cur is (select table_name from [email protected] 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 [email protected] 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 [email protected] 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 [email protected] 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

一兩件事,看起來錯誤是prjt_name是一個字符串,但你把它作爲你的查詢號碼。因此,這裏就是我會改變:

|| 'where t.project_id = (select project_id from [email protected]_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 [email protected]_pos15 where project_name = upper(''' 
    || prjt_name 
    || '''))' 

而且你必須刪除delete你從你的光標做......這看起來完全錯誤的。循環只是移動到下一個記錄

希望這會有所幫助。

0
declare 
     update_count integer := 0; 
     prjt_name varchar2(100) := '01213264B'; 
     tablename varchar2(100); 

     cursor my_cur is 
      select table_name 
       from [email protected]_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 [email protected]_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 [email protected]_pos15 where project_name = upper(' 
      || prjt_name 

可以替代由

where t.project_name = prjt_name 
+0

在連接顯示的「進入」行後,我仍然收到相同的錯誤消息。 – tparker

+0

爲什麼在聲明部分使用not null。這在plsql blok中不起作用。 – Valli

+0

我需要這個子查詢,因爲prjct_name是由用戶創建和提供的。但是我們的系統把這個名字變成了一個數字,這個數字就是用來識別變化的。我不能依賴最終用戶知道如何計算他們的項目編號。 – tparker

相關問題