PL/SQL错误过程、函数及解决方案笔记
1、創建一個過程,利用cursor將一個表中的兩個字段的內容,以循環的方式放到另外一個表中。
create or replace procedure copy_data_into_newTable
as
declare
f_name varchar(50);
l_name varchar(50);
cursor t_cursor is
select first_name,last_name from students;
begin
open t_cursor;
loop
fetch t_cursor into f_name,l_name;
exit when t_cursor%notfound;
insert into stu4 values(f_name,l_name);
exit loop;
close t_cursor;
end copy_data_into_newTable;
解決方法:
錯誤很簡單,把loop的循環尾由exit loop改為end loop問題解決。
2、創建一個過程,用以實現一個表的完全拷貝(包括結構及內容)
create or replace procedure copy_table(
old_table varchar(50),
new_table varchar(50)
)
as
declare
--sql_copy_structure varchar(500);
--sql_copy_date varchar(200);
sql_copy varchar(500);
begin
sql_copy:='create table '||new_table||' as select * from '||old_table;
execute immediate sql_copy;
end copy_table;
解決方法:
錯誤例舉:
1、參數只能申明類型,而不能夠申明大小
2、declare是匿名块使用的聲明,在過程和函數中不要用
3、表名不要聲明過大,oracle有規定,不能夠超30
更改后程序如下:
create or replace procedure copy_table(
old_table varchar2,
new_table varchar
)
as
--sql_copy_structure varchar(500);
--sql_copy_date varchar(200);
sql_copy varchar(100);
begin
sql_copy:='create table '||new_table||' as select * from '||old_table;
execute immediate sql_copy;
exception
--people using this procedure don't have the proviledge to run this procedure
when others then
raise;
end copy_table;
3、觸發器觸發的時候出錯
這個觸發器的功能就是希望把當前插入的數據顯示出來。
表結構:
id first_name .....等
觸發器如下:
create or replace trigger T
after insert on stu for each row
declare
ID int;
str_sql varchar(100);
begin
ID:=:new.id;
str_sql:='select * from stu where id='||ID;
execute immediate str_sql;
exception
when others then
raise;
end T;
編譯通過。
可是當我stu表中插入數據的時候:
insert into stu values(student_sequence.NEXTVAL,'Feng','Meteor','Technology',4,120);
報以我這樣的錯誤:
table HR.STU is mutating,trigger/function may not see it。
插入不成功。
請問是什么原因嗎?
謝謝解答!
4、存儲過程運行的時候出錯
編譯不出錯,可是運行出錯
create or replace procedure FLB_2
as
cursor c1 is
select cno from prd_flow_bak group by cno;
thcur c1%rowtype;
sql1 varchar(100);
begin
--if not c1%isopen then
open c1;
--end if;
loop
fetch c1 into thcur;
exit when c1%notfound;
sql1:='select * from prd_flow_bak where cno='||thcur.cno;
--dbms_output.put_line(thcur.cno);
execute immediate sql1;
exit;
end loop;
close c1;
end FLB_2;
運行出錯
begin
FLB_2;
end;
出錯提示如下:

什么原因呢?
解決:
本文针对PL/SQL中的过程与触发器编写时常见的错误进行了详细的解析,并提供了有效的解决方案。包括如何正确地使用循环结构、创建表的复制、解决触发表时的数据修改问题以及运行存储过程时可能遇到的陷阱。
341

被折叠的 条评论
为什么被折叠?



