2015-12-02 185 views
-1

在這個程序中我想插入userid的值是不一樣的,就像在這個select查詢中我們插入所有的數據一樣。如何在Oracle 11g中使用遊標在表中插入數據?

T_ALLOCATIONCONFIG表的內容:VW_BATCH

id userid customerid subbatchtypeid 
---- -------- ------------ ---------------- 
1 29  10003  1 
1 30  10003  1 

內容:

batchid batchname subbatchtypeid customerid batchstatus_id 
--------- ----------- ---------------- ------------ ---------------- 
1   test  1    10003  1 
2   test1  1    10003  1 
3   test2  1    10003  1 
4   test3  1    10003  1 
5   test4  1    10003  1 
6   test5  1    10003  1 
7   test6  1    10003  1 
8   test7  1    10003  1 
9   test8  1    10003  1 
10  test9  1    10003  1 
11  test12  1    10003  1 

我想在T_BATCHALLOCATION表這種類型的結果:

id batchid customerid userid 
---- --------- ----------- -------- 
1 1   10003  29 
2 2   10003  29 
3 3   10003  29 
4 4   10003  29 
5 5   10003  29 
6 6   10003  30 
7 7   10003  30 
8 8   10003  30 
9 9   10003  30 
10 10  10003  30 

假設我們有10個記錄從vw_batch,所以我想插入5記錄機智h userid = 29userid = 30batchallocation表中的5條記錄。

如何編寫查詢或存儲過程來插入這種結果?

CREATE OR REPLACE PROCEDURE p_autoallocate_batches 
AS 
    BEGIN 
    DECLARE 
     --  v_coll_rules_id coll_rules.coll_rules_id%TYPE; 
     v_customer_id t_customers.customer_id%TYPE; 
     v_batchid  t_batch.batchid%TYPE; 
     v_user_id  t_users.user_id%TYPE; 
     c    types.cursortype; 

     CURSOR c_aa IS 
     SELECT b.customer_id, 
       b.batchid, 
       a.user_id 
     FROM vw_batch b 
     JOIN t_allocationconfig a 
      ON a.customer_id = b.customer_id 
     AND a.subbatchtypeid = b.subbatchtypeid 
     WHERE b.batchstatus = 'New' AND a.isactive = 'Y'; 

    BEGIN 
     -- Dbms_Output.Put_Line('Hello World'); 
     -- Insert Into Tmp (Column1) Values ('Started'); 

     OPEN c_aa; 

     LOOP 
     FETCH c_aa INTO v_customer_id, v_batchid, v_user_id; 
     EXIT WHEN c_aa%NOTFOUND; 

     IF (v_customer_id > 0 AND v_batchid > 0 AND v_user_id > 0) THEN 
      INSERT INTO t_batchallocation (customer_id, batchid, user_id, created_by, created_date) 
      VALUES (v_customer_id, v_batchid, v_user_id, 1, sysdate); 

      UPDATE t_batch 
      SET batchstatus_id = 2, modified_by = 1, modified_date = sysdate 
      WHERE batchid = v_batchid; 
     END IF; 

     --Insert Into Tmp (Column1) Values ('Started123'); 

     COMMIT; 
     END LOOP; 

     CLOSE c_aa; 

    END; 
    END; 
+0

你真的需要在** PL/SQL **中做到嗎?我認爲你可以使用** MERGE **語句在純** SQL **中一步完成。 –

+0

請給我例子使用MERGE。是我需要在oracle sql developer –

+0

SQL Developer是一個工具。 SQL是一種語言。無論如何,只要在網站上搜索MERGE語句,就會得到大量的例子。 –

回答

3

有沒有必要使用遊標來做到這一點 - 你可以做到這一點在兩個SQL語句像這樣(但你必須創建一個類來保存插入的batch_ids):

create type num_list as table of integer; 

create or replace procedure p_autoallocate_batches 
as 
    v_batch_ids num_list; 
begin 
    insert into t_batchallocation (customer_id, 
           batchid, 
           user_id, 
           created_by, 
           created_date) 
    select b.customer_id, 
     b.batchid, 
     a.user_id, 
     1, 
     sysdate 
    from vw_batch b 
     inner join t_allocationconfig a on (a.customer_id = b.customer_id 
              and a.subbatchtypeid = b.subbatchtypeid) 
    where b.batchstatus = 'New' 
    and a.isactive = 'Y' 
    and b.customer_id > 0 
    and b.batchid > 0 
    and a.user_id > 0 
    returning batchid bulk collect into v_batch_ids; 

    update t_batch 
    set batchstatus_id = 2, modified_by = 1, modified_date = sysdate 
    where batchid in (select * from table(v_batch_ids)); 

    commit; 

end p_autoallocate_batches; 
/

NB未經測試,因爲您沒有提供表格定義或樣本數據

此外,我不確定我是否回答了您原來的問題,因爲我不明白。請修改您的問題,以便在表格中提供一些示例數據,並以表格格式插入您想要的內容。


好吧,下面加入到你的問題的其他信息,我覺得下面會做你以後:

with t_allocationconfig as (select 1 id, 29 userid, 10003 customerid, 1 subbatchtypeid from dual union all 
          select 1 id, 30 userid, 10003 customerid, 1 subbatchtypeid from dual), 
       vw_batch as (select 1 batchid, 'test' batchname, 1 subbatchtypeid, 10003 customerid, 1 batchstatus_id from dual union all 
          select 2 batchid, 'test1' batchname, 1 subbatchtypeid, 10003 customerid, 1 batchstatus_id from dual union all 
          select 3 batchid, 'test2' batchname, 1 subbatchtypeid, 10003 customerid, 1 batchstatus_id from dual union all 
          select 4 batchid, 'test3' batchname, 1 subbatchtypeid, 10003 customerid, 1 batchstatus_id from dual union all 
          select 5 batchid, 'test4' batchname, 1 subbatchtypeid, 10003 customerid, 1 batchstatus_id from dual union all 
          select 6 batchid, 'test5' batchname, 1 subbatchtypeid, 10003 customerid, 1 batchstatus_id from dual union all 
          select 7 batchid, 'test6' batchname, 1 subbatchtypeid, 10003 customerid, 1 batchstatus_id from dual union all 
          select 8 batchid, 'test7' batchname, 1 subbatchtypeid, 10003 customerid, 1 batchstatus_id from dual union all 
          select 9 batchid, 'test8' batchname, 1 subbatchtypeid, 10003 customerid, 1 batchstatus_id from dual union all 
          select 10 batchid, 'test9' batchname, 1 subbatchtypeid, 10003 customerid, 1 batchstatus_id from dual union all 
          select 11 batchid, 'test12' batchname, 1 subbatchtypeid, 10003 customerid, 1 batchstatus_id from dual) 
--end of mimicking your sample data - see SQL below: 
select vb.batchid id, -- is id supposed to be derived using a sequence, perhaps? 
     vb.batchid, 
     vb.customerid, 
     ta.userid 
from (select id, 
       userid, 
       customerid, 
       subbatchtypeid, 
       row_number() over (partition by id, customerid, subbatchtypeid order by userid) rn, 
       count(*) over (partition by id, customerid, subbatchtypeid) cnt 
     from t_allocationconfig) ta 
     inner join (select batchid, 
          batchname, 
          subbatchtypeid, 
          customerid, 
          batchstatus_id, 
          row_number() over (partition by customerid, subbatchtypeid order by batchid) rn, 
          count(*) over (partition by customerid, subbatchtypeid) cnt 
        from vw_batch) vb on (ta.customerid = vb.customerid 
              and ta.subbatchtypeid = vb.subbatchtypeid 
              and ta.rn = ceil(vb.rn/ceil(vb.cnt/ta.cnt))); 

     ID BATCHID CUSTOMERID  USERID 
---------- ---------- ---------- ---------- 
     1   1  10003   29 
     2   2  10003   29 
     3   3  10003   29 
     4   4  10003   29 
     5   5  10003   29 
     6   6  10003   29 
     7   7  10003   30 
     8   8  10003   30 
     9   9  10003   30 
     10   10  10003   30 
     11   11  10003   30 

這意味着你的代碼應該是沿東西線作者:

-- make sure you have a global type: 
create type num_list as table of integer; 

create or replace procedure p_autoallocate_batches 
as 
    v_batch_ids num_list; 
begin 
    insert into t_batchallocation (customer_id, 
           batchid, 
           user_id, 
           created_by, 
           created_date) 
    select vb.customerid, 
     vb.batchid, 
     ta.userid, 
     1, 
     sysdate 
    from (select id, 
       userid, 
       customerid, 
       subbatchtypeid, 
       row_number() over (partition by id, customerid, subbatchtypeid order by userid) rn, 
       count(*) over (partition by id, customerid, subbatchtypeid) cnt 
      from t_allocationconfig) ta 
     inner join (select batchid, 
          batchname, 
          subbatchtypeid, 
          customerid, 
          batchstatus_id, 
          row_number() over (partition by customerid, subbatchtypeid order by batchid) rn, 
          count(*) over (partition by customerid, subbatchtypeid) cnt 
        from vw_batch) vb on (ta.customerid = vb.customerid 
              and ta.subbatchtypeid = vb.subbatchtypeid 
              and ta.rn = ceil(vb.rn/ceil(vb.cnt/ta.cnt))) 
    returning batchid bulk collect into v_batch_ids; 

    update t_batch 
    set batchstatus_id = 2, modified_by = 1, modified_date = sysdate 
    where batchid in (select * from table(v_batch_ids)); 

    commit; 

end p_autoallocate_batches; 
/

NB你可能不得不玩弄partition by並加入子句,以確保數據被分組並加入到正確的列中 - 我猜測了它背後的邏輯是什麼,因爲你沒有說。希望你能夠解構查詢,看看它在做什麼,並根據需要進行修改!

+0

thx boneist爲您的答覆,但它不能解決我的問題我讀你不明白我的問題,所以我可以編輯我的問題,如果你知道然後回覆它非常緊迫 –

+1

你需要提供相應的數據從你的vw_batch,連同您希望看到的數據。請記住,我們無法看到您的屏幕,所以如果您希望我們幫助您,您需要提供儘可能多的信息,以便我們可以複製您的數據等並針對它執行查詢。 – Boneist

+0

@DipalKothari我已經更新了我的答案,以包含SQL語句,我相信你做了之後,假設我理解你的需求當然!感謝您將信息添加到問題中 - 這讓我們更容易看到您想要實現的目標! – Boneist

相關問題