2014-02-12 43 views
0

使用Oracle PL/SQL,如何使用多個值爲IN clause填充bind variable :b3? (此代碼是僅用於演示目的 - 它可能無法編譯,但它確實澄清問題如有需要)如何使用動態PL/SQL與IN子句中的值的字符串?

declare 
     type work_rec is record ( 
     work_status varchar2(50), 
     work_cd  varchar2(50)); 

    type work_tab is table of work_rec index by pls_integer; 

    t_work_tab work_tab; 
    sql_stmt varchar2(400); 

begin 
    select case  
    when status_desc like '%Employed%' then 'Employed' 
    else 'Unknown' 
    end as work_status 
    ,case 
    when status_cd between '1' and '9' then '1,2,3,4' 
    else '0' 
    end as work_cd 
    bulk collect into t_work_tab 
    from employee_table; 

    for i in t_work_tab.first..t_work_tab.last 
    loop 
    sql_stmt := 'insert into employee_hist 
       select name, 
         employer 
       from tax_table 
       where employment_cd in (:b3)'; --< how to populate this with '1','2','3','4' 

    execute immediate sql_stmt using t_work_tab(i).work_cd; 
    commit; 

    end loop; 

end; 
/
+2

這個問題已經回答了很多次 - HTTP:/ /tkyte.blogspot.com/2006/06/varying-in-lists.html – OldProgrammer

+0

謝謝你的鏈接,但它不是我想要的。 :-) – tale852150

回答

0

更好的解決方案(至少對我的問題) - 不使用綁定變量,而是串聯值的字符串(只顯示代碼段):

for i in t_work_tab.first..t_work_tab.last 
    loop 
    sql_stmt := 'insert into employee_hist 
      select name, 
        employer 
      from tax_table 
      where employment_cd in (' || t_work_tab(i).work_cd || ')'; 

    execute immediate sql_stmt; 
... 

你的想法。感謝您的輸入。

1

當你遍歷價值觀,不斷追加與「字符串,並根據需要組成英寸的部分。然後,您可以使用該字符串作爲您的SQL語句的一部分。

temp = "'" 
Loop through values a,b,c,d as str 
    temp = temp + str + "'", 
End Loop 
temp = substr(temp,0,length(temp)) // this is to trim the last , character 

希望它能幫助!

1

您需要在循環內使用另一個變量,然後在其外部的語句中準備列表。像這樣的東西可能會有所幫助:

declare 
     type work_rec is record ( 
     work_status varchar2(50), 
     work_cd  varchar2(50)); 

    type work_tab is table of work_rec index by pls_integer; 

    t_work_tab work_tab; 
    sql_stmt VARCHAR2(400); 
    v_in_str varchar2(100);_ 

begin 
    select case  
    when status_desc like '%Employed%' then 'Employed' 
    else 'Unknown' 
    end as work_status 
    ,case 
    when status_cd between '1' and '9' then '1,2,3,4' 
    else '0' 
    end as work_cd 
    bulk collect into t_work_tab 
    from employee_table; 

    for i in t_work_tab.first..t_work_tab.last 
    loop 
    v_in_str := v_in_str || t_work_tab(i).work_cd || ','; 

    END loop; 
    v_in_str :=rtrim(v_in_str, ','); 
    sql_stmt := 'insert into employee_hist 
       select name, 
         employer 
       from tax_table 
       where employment_cd in ('||v_in_str||')'; 

    execute immediate sql_stmt; 
    commit; 

end; 
1

您可以使用PLSQL集合作爲綁定變量,這似乎是更好的解決方案對我說:

declare 
     type t_nbr_tbl is table of number; 

     type work_rec is record ( 
      work_status varchar2(50), 
      work_cd  t_nbr_tbl; 

     type work_tab is table of work_rec index by pls_integer; 

     t_work_tab work_tab; 
     sql_stmt varchar2(400); 

    begin 
     select case  
      when status_desc like '%Employed%' then 'Employed' 
      else 'Unknown' 
     end as work_status 
     ,case 
      when status_cd between '1' and '9' then t_nbr_tbl(1,2,3,4) 
      else t_nbr_tbl(0) 
     end as work_cd 
     bulk collect into t_work_tab 
     from employee_table; 

     for i in t_work_tab.first..t_work_tab.last 
     loop 
      sql_stmt := 'insert into employee_hist 
         select name, 
           employer 
         from tax_table 
         where employment_cd in (select column_value from table(:b3))'; --< how to populate this with '1','2','3','4' 

      execute immediate sql_stmt using t_work_tab(i).work_cd; 
      commit; 

     end loop; 

    end; 
    /
相關問題