Oracle 存储过程纠错
( V_PageNo in int,V_PageSize in int,V_Sql in nvarchar,V_Parms in nvarchar,V_OrderBy in nvarchar,
V_TotalRow out int, V_PageCount out int
)
AS
V_ErrorMessage nvarchar;
V_ErrorSeverity INT;
V_ErrorState INT;
V_ERROR_LINE INT;
V_ERROR_NUMBER INT;
V_ERROR_MESSAGE nvarchar;
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; &nb