在這個程序中我想插入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 = 29
和userid = 30
在batchallocation
表中的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;
你真的需要在** PL/SQL **中做到嗎?我認爲你可以使用** MERGE **語句在純** SQL **中一步完成。 –
請給我例子使用MERGE。是我需要在oracle sql developer –
SQL Developer是一個工具。 SQL是一種語言。無論如何,只要在網站上搜索MERGE語句,就會得到大量的例子。 –