存储过程的调试通常采用两种方法。
1.
设置
debug file ,
以
trace
方式。
2.
用
return …… with resume
方式调试。
例:
1
create procedure getday(t_day date,t_int int ,t_flag char(10)) returning date;
define i int;
define tt_day date;
DEFINE ESQL,EISAM INT;
DEFINE ETEXT CHAR(80);
Set debug file to “sun.tmp”;
let tt_day="18991231";
let i=0;
if t_flag="year" then
while 1=1
on exception in(-1267)
let i=i+1;
end exception ;
let tt_day=t_day-i;
let tt_day=tt_day+t_int units year;
trace tt_day;
exit while;
end while;
elif t_flag="month" then
while 1=1
on exception in(-1267)
let i=i+1;
end exception ;
let tt_day=t_day-i;
let tt_day=tt_day+t_int units month;
trace tt_day;
exit while;
end while;
elif t_flag="day" then
let tt_day=t_day+t_int units day;
else
let tt_day=t_day;
end if;
return tt_day;
end procedure;
例:
2
create procedure getday(t_day date,t_int int ,t_flag char(10)) returning date;
define i int;
define tt_day date;
DEFINE ESQL,EISAM INT;
DEFINE ETEXT CHAR(80);
let tt_day="18991231";
let i=0;
if t_flag="year" then
while 1=1
on exception in(-1267)
let i=i+1;
end exception ;
let tt_day=t_day-i;
let tt_day=tt_day+t_int units year;
return tt_day with resume;
exit while;
end while;
elif t_flag="month" then
while 1=1
on exception in(-1267)
let i=i+1;
end exception ;
let tt_day=t_day-i;
let tt_day=tt_day+t_int units month;
return tt_day with resume;
exit while;
end while;
elif t_flag="day" then
let tt_day=t_day+t_int units day;
else
let tt_day=t_day;
end if;
return tt_day;
end procedure;
其中重点提下跟踪调试语句trace
存储过程被正确创建后,说明无语法错误,但有可能有逻辑错误
TRACE 语句用于调试存储过程, 它可以跟踪以下存储过程实体的值:
变量( Variables)
过程参数 (Procedure arguments)
返回值 (Return values)
SQL 错误代码( SQL error codes)
ISAM 错误代码 (ISAM error codes)
TRACE 语句把跟踪结果写到一个文件中, 该文件由 SQL 语句 SET DEBUG FILE 指定
TRACE 语句用于调试存储过程, 它可以跟踪以下存储过程实体的值:
变量( Variables)
过程参数 (Procedure arguments)
返回值 (Return values)
SQL 错误代码( SQL error codes)
ISAM 错误代码 (ISAM error codes)
TRACE 语句把跟踪结果写到一个文件中, 该文件由 SQL 语句 SET DEBUG FILE 指定
TRACE
语句的三种形式:
TRACE ON :打开跟踪调试, 跟踪所有语句
TRACE OFF :关闭跟踪调试
TRACE PROCEDURE : 对于过程调用, 不跟踪过程体,仅跟踪过程的输入和返回值。
TRACE ON :打开跟踪调试, 跟踪所有语句
TRACE OFF :关闭跟踪调试
TRACE PROCEDURE : 对于过程调用, 不跟踪过程体,仅跟踪过程的输入和返回值。
create procedure tracepro(var_user_num int)
define var_user_date date;
set debug file to "/export/home/user/trace";
-- 设置输出文件
trace on;
-- 跟踪所有执行的语句
select user_date into var_user_date
from users
where user_num = var_user_num;
if var_user_date is null then
trace "user date is null";
-- 执行到这里输出 user date is null
execute procedure other((var_user_num );
end if;
trace off;
-- 关闭跟踪
end procedure;
define var_user_date date;
set debug file to "/export/home/user/trace";
-- 设置输出文件
trace on;
-- 跟踪所有执行的语句
select user_date into var_user_date
from users
where user_num = var_user_num;
if var_user_date is null then
trace "user date is null";
-- 执行到这里输出 user date is null
execute procedure other((var_user_num );
end if;
trace off;
-- 关闭跟踪
end procedure;