总共有三篇:
3:就是本篇了:折腾Oracle问题小菜记(三)
本篇又有新突破,再记录一下:
1:自己写了一条分页存储过程,也是CYQ.Data默认产生的存储过程:
create
or
replace
packageMyPackage
as
typeMyCursor is ref cursor ;
procedure SelectBase(pageIndex int ,pageSize int ,tableName varchar2 ,whereStr varchar2 ,
resultCountout int ,resultCursoroutMyCursor);
end MyPackage;
create or replace packageBodyMyPackage is
procedure SelectBase(pageIndex int ,resultCursoroutMyCursor)
is
-- 定义变量
newtableName varchar2 ( 4000 );
rowStart int ;
rowEnd int ;
MysqL varchar2 ( 8000 );
whereOnly varchar2 ( 8000 );
OrderOnly varchar2 ( 400 );
begin
newtableName: = tableName;
MysqL: = ' selectcount(*)from ' || tableName;
if whereStr is not null and length(whereStr) > 0
then
rowStart: = instr(whereStr, ' orderby ' );
if rowStart > 0
then
whereOnly: = substr(whereStr, 1 ,rowStart - 1 ); -- 取得条件
OrderOnly: = substr(whereStr,rowStart,length(whereStr) - rowStart + 1 ); -- 取得排序方式(orderby字段方式)
else
whereOnly: = whereStr;
OrderOnly: = '' ;
end if ;
whereOnly: = ' where ' || whereOnly;
MysqL: = MysqL || whereOnly;
end if ;
execute immediateMysqL into resultCount;
-- dbms_output.put_line('查询总条数sql=>'||whereStr||'--'||MysqL||resultCount);
-- 执行查询,查询总条数
-- 不分页查所有
if pageIndex = 0 and pageSize = 0
then
MysqL: = ' select*from ' || tableName || whereOnly || OrderOnly;
else
-- 计算起始和结束索引
rowStart: = (pageIndex - 1 ) * pageSize + 1 ;
rowEnd: = rowStart + pageSize - 1 ;
MysqL: = ' select*from(selectt.*,RowNumasrnfrom(select*from ' || newtableName || whereOnly || OrderOnly || ' )t)wherernbetween ' || rowStart || ' and ' || rowEnd;
end if ;
open ResultCursor for MysqL;
-- dbms_output.put_line('sql=>'||MysqL);
end SelectBase;
end MyPackage;
typeMyCursor is ref cursor ;
procedure SelectBase(pageIndex int ,pageSize int ,tableName varchar2 ,whereStr varchar2 ,
resultCountout int ,resultCursoroutMyCursor);
end MyPackage;
create or replace packageBodyMyPackage is
procedure SelectBase(pageIndex int ,resultCursoroutMyCursor)
is
-- 定义变量
newtableName varchar2 ( 4000 );
rowStart int ;
rowEnd int ;
MysqL varchar2 ( 8000 );
whereOnly varchar2 ( 8000 );
OrderOnly varchar2 ( 400 );
begin
newtableName: = tableName;
MysqL: = ' selectcount(*)from ' || tableName;
if whereStr is not null and length(whereStr) > 0
then
rowStart: = instr(whereStr, ' orderby ' );
if rowStart > 0
then
whereOnly: = substr(whereStr, 1 ,rowStart - 1 ); -- 取得条件
OrderOnly: = substr(whereStr,rowStart,length(whereStr) - rowStart + 1 ); -- 取得排序方式(orderby字段方式)
else
whereOnly: = whereStr;
OrderOnly: = '' ;
end if ;
whereOnly: = ' where ' || whereOnly;
MysqL: = MysqL || whereOnly;
end if ;
execute immediateMysqL into resultCount;
-- dbms_output.put_line('查询总条数sql=>'||whereStr||'--'||MysqL||resultCount);
-- 执行查询,查询总条数
-- 不分页查所有
if pageIndex = 0 and pageSize = 0
then
MysqL: = ' select*from ' || tableName || whereOnly || OrderOnly;
else
-- 计算起始和结束索引
rowStart: = (pageIndex - 1 ) * pageSize + 1 ;
rowEnd: = rowStart + pageSize - 1 ;
MysqL: = ' select*from(selectt.*,RowNumasrnfrom(select*from ' || newtableName || whereOnly || OrderOnly || ' )t)wherernbetween ' || rowStart || ' and ' || rowEnd;
end if ;
open ResultCursor for MysqL;
-- dbms_output.put_line('sql=>'||MysqL);
end SelectBase;
end MyPackage;
执行测试语句:
declare
ResultCursorMyPackage.MyCursor;
ResultCount int ;
begin
MyPackage.SelectBase( 1 , 2 , ' USERS ' , ' id>1orderbyid ' ,ResultCount,ResultCursor);
end ;
ResultCursorMyPackage.MyCursor;
ResultCount int ;
begin
MyPackage.SelectBase( 1 , 2 , ' USERS ' , ' id>1orderbyid ' ,ResultCount,ResultCursor);
end ;
说明:
为写这段存储过程历经了半天,需要看语法,又要调试,最后采用步步注释法才一条语句一条语句的写到最后。
测试调试也弄了半天,要定义游标传进去才行。
测试调试也弄了半天,要定义游标传进去才行。
继续说明:
select
COLUMN_NAME
as
ColumnName,
Data_length * 2 as MaxSize,
case NULLABLE when ' Y ' then 1 else 0 end as IsNullable,
0 as ReadOnly,
DATA_TYPE as sqlType
from USER_TAB_COLS where TABLE_NAME = upper (:TableName) order by COLUMN_ID
Data_length * 2 as MaxSize,
case NULLABLE when ' Y ' then 1 else 0 end as IsNullable,
0 as ReadOnly,
DATA_TYPE as sqlType
from USER_TAB_COLS where TABLE_NAME = upper (:TableName) order by COLUMN_ID
3:存储过程参数放在另一个表,独立查询:
select
argument_Name
as
ColumnName,
-
1
as
MaxSize,
0
as
IsNullable,
0
as
ReadOnly,
'
int
'
as
sqlType
from
user_arguments
where
object_name
=
upper
(:TableName)
4:查询所有表/视图/存储过程
Select
object_name
From
user_objects
Where
object_type
=
'
TRIGGER
'
;
--
所有触发器
Select object_name From user_objects Where object_type = ' PROCEDURE ' ; -- 所有存储过程
Select object_name From user_objects Where object_type = ' VIEW ' ; -- 所有视图
Select object_name From user_objects Where object_type = ' TABLE ' ; -- 所有表
Select object_name From user_objects Where object_type = ' PROCEDURE ' ; -- 所有存储过程
Select object_name From user_objects Where object_type = ' VIEW ' ; -- 所有视图
Select object_name From user_objects Where object_type = ' TABLE ' ; -- 所有表