微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

折腾Oracle问题小菜记[分页存储过程/查询所有表、视图、存储过程/查询表、视图、存储过程字段结构与参数](三)

说明:
为了让CYQ.Data框架支持Oracle,这几天对Oracle进行了基本探索,并把中间遇到的问题轻轻的记录了下来,与大伙共享。

总共有三篇:

1:初折腾Oracle问题小记

2:初折腾Oracle问题小记二

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;

执行测试语句:

declare
ResultCursorMyPackage.MyCursor;
ResultCount
int ;
begin
MyPackage.SelectBase(
1 , 2 , ' USERS ' , ' id>1orderbyid ' ,ResultCount,ResultCursor);
end ;

说明:

为写这段存储过程历经了半天,需要看语法,又要调试,最后采用步步注释法才一条语句一条语句的写到最后。
测试调试也弄了半天,要定义游标传进去才行。

继续说明:

Oracle里的存储过程的可以有Package,等于一个名称空间了。

存储过程的代码里面有几个小问题,oracle->比较->mssql

1:||为链接符号即+号

2:instr函数sql函数charindex函数一样,只是里面的头两个参数的顺序要反过来。

3:substr函数sql函数substring函数一样。

4:length函数sql函数len函数一样。

5:if...then...elseendif,mssql里为ifbeginend

6:“;"号一行语句一个,mssql里没有。

2:数据库表/视图的字段结构查询

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

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 ' ; -- 所有表

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐