2016-04-18 24 views
0

2つの表に複数の行を挿入するSQLを作成しました。しかし、最初の行を最初のテーブルに挿入するだけです。誰でもこの機会に私を助けてくれますか?ここでは、以下の::oracleは、1つの挿入クエリを使用して複数の表に複数の行を挿入します。

私の試み私の手順>>

PROCEDURE NEW_USER_PRIVELEGE_HIERCHY 
(
    IN_CREATED_BY    IN VARCHAR2, 
    IN_MODIFIED_BY       IN VARCHAR2, 
    IN_USERNAME       IN VARCHAR2 
) 
AS 
    L_USER_ID INTEGER; 
    L_PRIVILEGE_ID INTEGER; 
    L_HIERCHY_ID INTEGER; 
    N_USER_ID INTEGER := hibernate_sequence.NEXTVAL; 
BEGIN 
    SELECT case 
     when exists(SELECT USER_ID FROM T_MT_USER WHERE USERNAME = IN_USERNAME) 
     then 
       (SELECT USER_ID FROM T_MT_USER WHERE USERNAME = IN_USERNAME) 
     else 0 
     end into L_USER_ID 
     FROM dual; 

    IF (L_USER_ID = 0) THEN 
     INSERT INTO T_MT_USER 
     (USER_ID, ACCOUNT_EXPIRED_FLG, ACCOUNT_LOCKED_FLG, created_by, CREATED_TS, ACTIVE_FLG, end_effective_dt, modified_by, MODIFIED_TS, "password", password_expired, start_effective_dt, username) 
     VALUES 
     (N_USER_ID, 0, 0, IN_CREATED_BY, SYSTIMESTAMP, 1, NULL, IN_MODIFIED_BY, SYSTIMESTAMP, '$2a$10$3hoAeeVJ0AG.CQS2xX8rT.wUlt1rjoiYsHW0YflZEiQ5QYzG4HpYi', 0, SYSTIMESTAMP, IN_USERNAME); -- for Synergy1! 
     -- start inserting in privilege talbe 
     insert into T_MT_USER_PRIVILEGE (privilege_id,user_id, created_by, CREATED_TS, modified_by,MODIFIED_TS) values (8001,N_USER_ID, IN_CREATED_BY, sysdate, IN_MODIFIED_BY, sysdate); 
     insert into T_MT_USER_PRIVILEGE (privilege_id,user_id, created_by, CREATED_TS, modified_by,MODIFIED_TS) values (8003,N_USER_ID, IN_CREATED_BY, sysdate, IN_MODIFIED_BY, sysdate); 
     insert into T_MT_USER_PRIVILEGE (privilege_id,user_id, created_by, CREATED_TS, modified_by,MODIFIED_TS) values (8004,N_USER_ID, IN_CREATED_BY, sysdate, IN_MODIFIED_BY, sysdate); 
     insert into T_MT_USER_PRIVILEGE (privilege_id,user_id, created_by, CREATED_TS, modified_by,MODIFIED_TS) values (8007,N_USER_ID, IN_CREATED_BY, sysdate, IN_MODIFIED_BY, sysdate); 
     insert into T_MT_USER_PRIVILEGE (privilege_id,user_id, created_by, CREATED_TS, modified_by,MODIFIED_TS) values (8010,N_USER_ID, IN_CREATED_BY, sysdate, IN_MODIFIED_BY, sysdate); 
     -- start inserting in hierchy table 
     insert into T_MT_USER_HIER_NODE (hier_node_id, user_id, created_by, CREATED_TS, modified_by, MODIFIED_TS) values (101, N_USER_ID, IN_CREATED_BY, sysdate, IN_MODIFIED_BY, sysdate); 
     insert into T_MT_USER_HIER_NODE (hier_node_id, user_id, created_by, CREATED_TS, modified_by, MODIFIED_TS) values (102, N_USER_ID, IN_CREATED_BY, sysdate, IN_MODIFIED_BY, sysdate);  
     insert into T_MT_USER_HIER_NODE (hier_node_id, user_id, created_by, CREATED_TS, modified_by, MODIFIED_TS) values (103, N_USER_ID, IN_CREATED_BY, sysdate, IN_MODIFIED_BY, sysdate);  
     insert into T_MT_USER_HIER_NODE (hier_node_id, user_id, created_by, CREATED_TS, modified_by, MODIFIED_TS) values (5626, N_USER_ID, IN_CREATED_BY, sysdate, IN_MODIFIED_BY, sysdate);  
     insert into T_MT_USER_HIER_NODE (hier_node_id, user_id, created_by, CREATED_TS, modified_by, MODIFIED_TS) values (6548, N_USER_ID, IN_CREATED_BY, sysdate, IN_MODIFIED_BY, sysdate);   
    END IF; 
EXCEPTION 
    WHEN OTHERS THEN 
     log_api.fatal(log_ctx,'Exception in NEW_USER_PRIVELEGE_HIERCHY'); 
     RAISE; 
END NEW_USER_PRIVELEGE_HIERCHY; 

は、私があまりにもinsert allが、運を試してみましたが、私のプロシージャの呼び出し>>

execute USER_DEMO.NEW_USER('sbappi', 'sbappi', 'sumon8'); 

です。

+0

プロシージャコールでエラーが発生しましたか。タイトルによれば、私は他の製品のタグを削除しました。 – Aleksej

+0

いいえ、エラーは表示されません –

+1

ここで 'log_ctx'が宣言されていますか? –

答えて

0

SQLデベロッパーからテストする場合、dbms loggerを使用して挿入カウントを取得し、失敗した箇所を取得できます。このスニペットが役立つことを願っています。

PROCEDURE NEW_USER_PRIVELEGE_HIERCHY(
    IN_CREATED_BY IN VARCHAR2, 
    IN_MODIFIED_BY IN VARCHAR2, 
    IN_USERNAME IN VARCHAR2) 
AS 
    L_USER_ID  INTEGER; 
    L_PRIVILEGE_ID INTEGER; 
    L_HIERCHY_ID INTEGER; 
    N_USER_ID  INTEGER := hibernate_sequence.NEXTVAL; 
    lv_cnt PLS_INTEGER; 
BEGIN 
    SELECT COUNT(1) 
    INTO L_USER_ID 
    FROM T_MT_USER 
    WHERE UPPER(USERNAME) = UPPER(IN_USERNAME); 
    IF (L_USER_ID   = 0) THEN 
    INSERT 
    INTO T_MT_USER 
     (
     USER_ID, 
     ACCOUNT_EXPIRED_FLG, 
     ACCOUNT_LOCKED_FLG, 
     created_by, 
     CREATED_TS, 
     ACTIVE_FLG, 
     end_effective_dt, 
     modified_by, 
     MODIFIED_TS, 
     "password", 
     password_expired, 
     start_effective_dt, 
     username 
    ) 
     VALUES 
     (
     N_USER_ID, 
     0, 
     0, 
     IN_CREATED_BY, 
     SYSTIMESTAMP, 
     1, 
     NULL, 
     IN_MODIFIED_BY, 
     SYSTIMESTAMP, 
     '$2a$10$3hoAeeVJ0AG.CQS2xX8rT.wUlt1rjoiYsHW0YflZEiQ5QYzG4HpYi', 
     0, 
     SYSTIMESTAMP, 
     IN_USERNAME 
    ); -- for Synergy1! 
    lv_cnt:=SQL%ROWCOUNT; 
    dbms_output.put_line(lv_cnt||' T_MT_USER '); 
    -- start inserting in privilege talbe 
    INSERT 
    INTO T_MT_USER_PRIVILEGE 
     (
     privilege_id, 
     user_id, 
     created_by, 
     CREATED_TS, 
     modified_by, 
     MODIFIED_TS 
    ) 
     VALUES 
     (
     8001, 
     N_USER_ID, 
     IN_CREATED_BY, 
     sysdate, 
     IN_MODIFIED_BY, 
     sysdate 
    ); 
    lv_cnt:=lv_cnt+SQL%ROWCOUNT; 
    dbms_output.put_line(lv_cnt||' T_MT_USER_PRIVILEGE '); 
    INSERT 
    INTO T_MT_USER_PRIVILEGE 
     (
     privilege_id, 
     user_id, 
     created_by, 
     CREATED_TS, 
     modified_by, 
     MODIFIED_TS 
    ) 
     VALUES 
     (
     8003, 
     N_USER_ID, 
     IN_CREATED_BY, 
     sysdate, 
     IN_MODIFIED_BY, 
     sysdate 
    ); 
    lv_cnt:=lv_cnt+SQL%ROWCOUNT; 
    dbms_output.put_line(lv_cnt||' T_MT_USER_PRIVILEGE '); 
    INSERT 
    INTO T_MT_USER_PRIVILEGE 
     (
     privilege_id, 
     user_id, 
     created_by, 
     CREATED_TS, 
     modified_by, 
     MODIFIED_TS 
    ) 
     VALUES 
     (
     8004, 
     N_USER_ID, 
     IN_CREATED_BY, 
     sysdate, 
     IN_MODIFIED_BY, 
     sysdate 
    ); 
    lv_cnt:=lv_cnt+SQL%ROWCOUNT; 
    dbms_output.put_line(lv_cnt||' T_MT_USER_PRIVILEGE '); 
    INSERT 
    INTO T_MT_USER_PRIVILEGE 
     (
     privilege_id, 
     user_id, 
     created_by, 
     CREATED_TS, 
     modified_by, 
     MODIFIED_TS 
    ) 
     VALUES 
     (
     8007, 
     N_USER_ID, 
     IN_CREATED_BY, 
     sysdate, 
     IN_MODIFIED_BY, 
     sysdate 
    ); 
    lv_cnt:=lv_cnt+SQL%ROWCOUNT; 
    dbms_output.put_line(lv_cnt||' T_MT_USER_PRIVILEGE '); 
    INSERT 
    INTO T_MT_USER_PRIVILEGE 
     (
     privilege_id, 
     user_id, 
     created_by, 
     CREATED_TS, 
     modified_by, 
     MODIFIED_TS 
    ) 
     VALUES 
     (
     8010, 
     N_USER_ID, 
     IN_CREATED_BY, 
     sysdate, 
     IN_MODIFIED_BY, 
     sysdate 
    ); 
    lv_cnt:=lv_cnt+SQL%ROWCOUNT; 
    dbms_output.put_line(lv_cnt||' T_MT_USER_PRIVILEGE '); 
    -- start inserting in hierchy table 
    INSERT 
    INTO T_MT_USER_HIER_NODE 
     (
     hier_node_id, 
     user_id, 
     created_by, 
     CREATED_TS, 
     modified_by, 
     MODIFIED_TS 
    ) 
     VALUES 
     (
     101, 
     N_USER_ID, 
     IN_CREATED_BY, 
     sysdate, 
     IN_MODIFIED_BY, 
     sysdate 
    ); 
    lv_cnt:=lv_cnt+SQL%ROWCOUNT; 
    dbms_output.put_line(lv_cnt||' T_MT_USER_HIER_NODE '); 
    INSERT 
    INTO T_MT_USER_HIER_NODE 
     (
     hier_node_id, 
     user_id, 
     created_by, 
     CREATED_TS, 
     modified_by, 
     MODIFIED_TS 
    ) 
     VALUES 
     (
     102, 
     N_USER_ID, 
     IN_CREATED_BY, 
     sysdate, 
     IN_MODIFIED_BY, 
     sysdate 
    ); 
    lv_cnt:=lv_cnt+SQL%ROWCOUNT; 
    dbms_output.put_line(lv_cnt||' T_MT_USER_HIER_NODE '); 
    INSERT 
    INTO T_MT_USER_HIER_NODE 
     (
     hier_node_id, 
     user_id, 
     created_by, 
     CREATED_TS, 
     modified_by, 
     MODIFIED_TS 
    ) 
     VALUES 
     (
     103, 
     N_USER_ID, 
     IN_CREATED_BY, 
     sysdate, 
     IN_MODIFIED_BY, 
     sysdate 
    ); 
    lv_cnt:=lv_cnt+SQL%ROWCOUNT; 
    dbms_output.put_line(lv_cnt||' T_MT_USER_HIER_NODE '); 
    INSERT 
    INTO T_MT_USER_HIER_NODE 
     (
     hier_node_id, 
     user_id, 
     created_by, 
     CREATED_TS, 
     modified_by, 
     MODIFIED_TS 
    ) 
     VALUES 
     (
     5626, 
     N_USER_ID, 
     IN_CREATED_BY, 
     sysdate, 
     IN_MODIFIED_BY, 
     sysdate 
    ); 
    lv_cnt:=lv_cnt+SQL%ROWCOUNT; 
    dbms_output.put_line(lv_cnt||' T_MT_USER_HIER_NODE '); 
    INSERT 
    INTO T_MT_USER_HIER_NODE 
     (
     hier_node_id, 
     user_id, 
     created_by, 
     CREATED_TS, 
     modified_by, 
     MODIFIED_TS 
    ) 
     VALUES 
     (
     6548, 
     N_USER_ID, 
     IN_CREATED_BY, 
     sysdate, 
     IN_MODIFIED_BY, 
     sysdate 
    ); 
    lv_cnt:=lv_cnt+SQL%ROWCOUNT; 
    dbms_output.put_line(lv_cnt||' T_MT_USER_HIER_NODE '); 
    END IF; 
EXCEPTION 
WHEN OTHERS THEN 
    log_api.fatal(log_ctx,'Exception in NEW_USER_PRIVELEGE_HIERCHY'); 
    RAISE; 
END NEW_USER_PRIVELEGE_HIERCHY; 
関連する問題