我正在尝试使用rowtype帮助在备份表中插入数据,如下所示
declare
vl_bkp_rec schema.table1%ROWTYPE;
BEGIN
FOR cur_rec IN
(SELECT *
FROM schema.table1
WHERE column_1 ='3f1d6348-014e-1000-8461-700c000493e0'
AND primary_key_column NOT IN ('8dc81f6e-0156-1000-8291-700e000493e0')
)
LOOP
INSERT INTO schema.backup_table VALUES vl_bkp_rec;
END LOOP;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
lv_err_msg := SUBSTR(SQLERRM, 1, 2999);
DBMS_OUTPUT.PUT_LINE('Handled - error while executing script. =>'|| lv_err_msg );
ROLLBACK;
END;
/
我收到了以下错误
Handled - error while executing script. =>ORA-01400: cannot insert NULL into ("schema"."backup_table"."primary_key_column")
但table1
和backup_table
具有完全相同的结构。 (创建backup_table
如下)
CREATE TABLE schema.backup_table AS
(SELECT * FROM schema.table1 WHERE rownum <1
);
并选择上面使用的查询来获取有效数据。我在这里做错了什么?
答案 0 :(得分:1)
您需要以下面的方式使用您的变量。目前在您的代码中,声明为表类型的变量未被填充:请参阅下面的如何使用它。
declare
vl_bkp_rec table1%ROWTYPE;
BEGIN
FOR cur_rec IN
(SELECT *
FROM table1
)
LOOP
vl_bkp_rec:=cur_rec; --Assign values of the cursor variable to your variable
INSERT INTO backup_table VALUES vl_bkp_rec;
END LOOP;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
--lv_err_msg := SUBSTR(SQLERRM, 1, 2999); ---you need to decalre it befor using it
DBMS_OUTPUT.PUT_LINE('Handled - error while executing script. =>' );
ROLLBACK;
END;
/