读书人

Oracle 存储过程纠错解决方法

发布时间: 2013-03-26 21:09:08 作者: rapoo

Oracle 存储过程纠错
( V_PageNo in int,V_PageSizein int,V_Sql in nvarchar,V_Parms in nvarchar,V_OrderBy in nvarchar,
V_TotalRow out int,V_PageCount out int
)
AS

V_ErrorMessagenvarchar;
V_ErrorSeverityINT;
V_ErrorStateINT;
V_ERROR_LINEINT;
V_ERROR_NUMBERINT;
V_ERROR_MESSAGEnvarchar;

V_rowNoBegin int;
V_rowNoEnd int;

V_SQLString nvarchar;
V_SQLString_MAX nvarchar;
V_ParmDefinition nvarchar;


V_ErrorMessage := ' ';
V_ErrorSeverity := 12;
V_ErrorState := 1;


IF LOWER(SUBSTRING(LTRIM(V_Sql),1, 6)) <> 'select'
BEGIN
V_ErrorMessage := '参数 V_Sql 的值必须以 select 开头。 %s' ;
RAISE_APPLICATION_ERROR (-20001,V_ErrorMessage||V_Sql) ;
RETURN;
END
IF CHARINDEX ('order by', LOWER(V_Sql)) > 0
BEGIN
V_ErrorMessage := '参数 V_Sql 的值不能包含 order by 子句。 %s' ;
RAISE_APPLICATION_ERROR (-20002,V_ErrorMessage||V_Sql) ;
RETURN;
END

IF LEN(V_OrderBy) < 1
BEGIN
V_ErrorMessage := '参数 V_OrderBy 的值不能为空(它要用于 ROW_NUMBER 函数)。 %s' ;
RAISE_APPLICATION_ERROR (-20000,V_ErrorMessage||V_OrderBy) ;
RETURN;
END
IF CHARINDEX ('order by', LOWER(V_OrderBy)) > 0
BEGIN
V_ErrorMessage := '参数 V_OrderBy 的值不能包含‘ORDER BY 关键字’(它要用于 ROW_NUMBER 函数)。 %s' ;
RAISE_APPLICATION_ERROR (-20000,V_ErrorMessage||V_OrderBy) ;
RETURN;
END





V_rowNoBegin := (V_PageNo - 1) * V_PageSize ;
V_rowNoEnd := V_rowNoBegin + V_PageSize ;


V_TotalRow := 0 ;
V_SQLString := GsPLUS.Fun_Intra_FormatSQLParm(V_sql, V_Parms, V_OrderBy) ;
IF (UPPER(LEFT(V_SQLString, 5)) := 'ERROR')
BEGIN
print V_SQLString
V_ErrorMessage := 'V_ParmCount 中参数个数不一致。' + V_SQLString;
RAISE_APPLICATION_ERROR (V_ErrorMessage, V_ErrorSeverity, V_ErrorState) WITH NOWAIT;
RETURN;
END;
print V_SQLString

Begin Try
V_SQLString_MAX := ' V_TotalRow := (SELECT MAX(ROWNUM) FROM (' + V_SQLString + ') AS tab)' ;
EXECUTE sp_executesql
V_SQLString_MAX,
N'V_TotalRow int OUTPUT',
V_TotalRow OUTPUT

V_PageCount :=
(CASE WHEN (V_TotalRow % V_PageSize)<>0
THEN (V_TotalRow / V_PageSize + 1)
ELSE (V_TotalRow / V_PageSize)
END);

End Try
Begin Catch


V_PageCount := -1;
V_ErrorMessage := '计算记录总数时出错!V_OrderBy is %s\r\n error line: %d, error number:%d, error message:%s'
SELECT V_ERROR_LINE := ERROR_LINE(), V_ERROR_NUMBER := ERROR_NUMBER(), V_ERROR_MESSAGE := ERROR_MESSAGE()
RAISE_APPLICATION_ERROR (V_ErrorMessage, V_ErrorSeverity, V_ErrorState,
V_OrderBy, V_ERROR_LINE, V_ERROR_NUMBER, V_ERROR_MESSAGE) WITH NOWAIT;
RETURN;
End Catch;


Begin Try


V_SQLString := 'SELECT *,0 as RoleR, 0 as RoleU, 0 as RoleD FROM ('+V_SQLString + ') AS tab WHERE tab.ROWNUM > V_rowNoBegin and tab.ROWNUM <:= V_rowNoEnd'
V_ParmDefinition := N'V_rowNoBegin int, V_rowNoEnd int, V_PageNo int, V_PageSize int'
EXECUTE sp_executesql
V_SQLString,
V_ParmDefinition,
V_rowNoBegin, V_rowNoEnd,
V_PageNo, V_PageSize
End Try
Begin Catch

V_PageCount := -1;
V_ErrorMessage := '取记录集时出错!error line: %d, error number:%d, error message:%s'
SELECT V_ERROR_LINE := ERROR_LINE(), V_ERROR_NUMBER := ERROR_NUMBER(), V_ERROR_MESSAGE := ERROR_MESSAGE()
RAISE_APPLICATION_ERROR (V_ErrorMessage, V_ErrorSeverity, V_ErrorState,
V_OrderBy, V_ERROR_LINE, V_ERROR_NUMBER, V_ERROR_MESSAGE) ;
RETURN;
End Catch;

错误:
Line # = 21 Column # = 16 Error Text = PLS-00103: 出现符号 "="在需要下列之一时:
constant
exception <an identifier>
<a double-quoted delimited-identifier> table long double ref
char time timestamp interval date binary national character
nchar
符号 "<an identifier>" 被替换为 "=" 后继续。

Line # = 22 Column # = 17 Error Text = PLS-00103: 出现符号 "="在需要下列之一时:
constant
exception <an identifier>
<a double-quoted delimited-identifier> table long double ref
char time timestamp interval date binary national character
nchar
符号 "<an identifier>" 被替换为 "=" 后继续。

Line # = 23 Column # = 14 Error Text = PLS-00103: 出现符号 "="在需要下列之一时:
constant
exception <an identifier>
<a double-quoted delimited-identifier> table long double ref
char time timestamp interval date binary national character
nchar



[解决办法]
IF CHARINDEX ('order by', LOWER(V_Sql)) > 0
BEGIN
V_ErrorMessage := '参数 V_Sql 的值不能包含 order by 子句。 %s' ;
RAISE_APPLICATION_ERROR (-20002,V_ErrorMessage||V_Sql) ;
RETURN;
END

IF这么写 :

IF CHARINDEX ('order by', LOWER(V_Sql)) > 0
THEN
V_ErrorMessage := '参数 V_Sql 的值不能包含 order by 子句。 %s' ;
RAISE_APPLICATION_ERROR (-20002,V_ErrorMessage||V_Sql) ;

END IF;
我的异常网推荐解决方案:oracle存储过程,http://www.myexception.cn/oracle-develop/177537.html

读书人网 >oracle

热点推荐