我写了一个sql,我想在2个表中插入多行。但它只是在第一个表中插入第一行。有人可以帮我这个吗?以下是我的尝试::
我的程序>>
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;
致电我的程序>>
execute USER_DEMO.NEW_USER('sbappi', 'sbappi', 'sumon8');
我也试过了insert all
但没有运气。
答案 0 :(得分: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;