Description:
To make CYQ.Data framework supports Oracle, Oracle has been for the past few days to explore the basic, intermediate and the problems encountered by gently recorded and shared with everyone.
There are three:
1: Early toss Oracle issues a small note
2: Early Notes on two issues toss Oracle
3: is a Benpian: Oracle issues toss dishes mind (c)
Benpian there are new breakthroughs, re-record it:
1: wrote a paging stored procedures, stored procedure is CYQ.Data default generated:
create
or
replace
package MyPackage
as
type MyCursor is ref cursor ;
procedure SelectBase(pageIndex int ,pageSize int ,tableName varchar2 ,whereStr varchar2 ,
resultCount out int , resultCursor out MyCursor);
end MyPackage;
create or replace package Body MyPackage is
procedure SelectBase(pageIndex int ,pageSize int ,tableName varchar2 ,whereStr varchar2 ,
resultCount out int , resultCursor out MyCursor)
is
-- 定义变量
newtableName varchar2 ( 4000 );
rowStart int ;
rowEnd int ;
mySql varchar2 ( 8000 );
whereOnly varchar2 ( 8000 );
OrderOnly varchar2 ( 400 );
begin
newtableName: = tableName;
mySql: = ' select count(*) from ' || tableName;
if whereStr is not null and length(whereStr) > 0
then
rowStart: = instr(whereStr, ' order by ' );
if rowStart > 0
then
whereOnly: = substr(whereStr, 1 ,rowStart - 1 ); -- 取得条件
OrderOnly: = substr(whereStr,rowStart, length(whereStr) - rowStart + 1 ); -- 取得排序方式(order by 字段 方式)
else
whereOnly: = whereStr;
OrderOnly: = '' ;
End IF ; whereOnly: = ' WHERE ' || whereOnly; the mySql: = the mySql || whereOnly; End IF ; Execute immediate the mySql INTO resultCount; - DBMS_OUTPUT.PUT_LINE ( 'Query Total = number of the SQL> 'whereStr || ||' - '|| || resultCount the mySql); - execute query, the total number of - not all search tab IF the pageIndex = 0 and the pageSize =
0
then
mySql: = ' select * from ' || tableName || whereOnly || OrderOnly;
else
-- 计算起始和结束索引
rowStart: = (pageIndex - 1 ) * pageSize + 1 ;
rowEnd: = rowStart + pageSize - 1 ;
mySql: = ' select * from (select t.*,RowNum as rn from (select * from ' || newtableName || whereOnly || OrderOnly || ' ) t) where rn between ' || rowStart || ' and ' || rowEnd;
end if ;
open ResultCursor for mySql;
-- dbms_output.put_line('SQL=>'||mySql);
end SelectBase;
end MyPackage;
type MyCursor is ref cursor ;
procedure SelectBase(pageIndex int ,pageSize int ,tableName varchar2 ,whereStr varchar2 ,
resultCount out int , resultCursor out MyCursor);
end MyPackage;
create or replace package Body MyPackage is
procedure SelectBase(pageIndex int ,pageSize int ,tableName varchar2 ,whereStr varchar2 ,
resultCount out int , resultCursor out MyCursor)
is
-- 定义变量
newtableName varchar2 ( 4000 );
rowStart int ;
rowEnd int ;
mySql varchar2 ( 8000 );
whereOnly varchar2 ( 8000 );
OrderOnly varchar2 ( 400 );
begin
newtableName: = tableName;
mySql: = ' select count(*) from ' || tableName;
if whereStr is not null and length(whereStr) > 0
then
rowStart: = instr(whereStr, ' order by ' );
if rowStart > 0
then
whereOnly: = substr(whereStr, 1 ,rowStart - 1 ); -- 取得条件
OrderOnly: = substr(whereStr,rowStart, length(whereStr) - rowStart + 1 ); -- 取得排序方式(order by 字段 方式)
else
whereOnly: = whereStr;
OrderOnly: = '' ;
End IF ; whereOnly: = ' WHERE ' || whereOnly; the mySql: = the mySql || whereOnly; End IF ; Execute immediate the mySql INTO resultCount; - DBMS_OUTPUT.PUT_LINE ( 'Query Total = number of the SQL> 'whereStr || ||' - '|| || resultCount the mySql); - execute query, the total number of - not all search tab IF the pageIndex = 0 and the pageSize =
0
then
mySql: = ' select * from ' || tableName || whereOnly || OrderOnly;
else
-- 计算起始和结束索引
rowStart: = (pageIndex - 1 ) * pageSize + 1 ;
rowEnd: = rowStart + pageSize - 1 ;
mySql: = ' select * from (select t.*,RowNum as rn from (select * from ' || newtableName || whereOnly || OrderOnly || ' ) t) where rn between ' || rowStart || ' and ' || rowEnd;
end if ;
open ResultCursor for mySql;
-- dbms_output.put_line('SQL=>'||mySql);
end SelectBase;
end MyPackage;
Perform tests:
declare
ResultCursor MyPackage.MyCursor;
ResultCount int ;
begin
MyPackage.SelectBase( 1 , 2 , ' USERS ' , ' id>1 order by id ' ,ResultCount,ResultCursor);
end ;
ResultCursor MyPackage.MyCursor;
ResultCount int ;
begin
MyPackage.SelectBase( 1 , 2 , ' USERS ' , ' id>1 order by id ' ,ResultCount,ResultCursor);
end ;
Description:
After the stored procedure to write this for a long time, we need to look at the syntax, but also debugging, and finally a written statement to a statement adopted last comment step by step method.
Test Debugging also get a long time, to define a cursor pass in the job.
Test Debugging also get a long time, to define a cursor pass in the job.
Continue Description:
Package Oracle may have in the stored procedure, a name equal to the space.
Code stored procedure, there are several minor problems, oracle-> Comparative -> MSSQL:
. 1: || i.e. link symbol No. +
2: instr function charindex sql functions and functions of the same, but the order of the first two parameters of the inside to turn.
3: substr functions and function substring function sql same.
4: length len function function and function like sql.
5: if ... then ... else end if, mssql there is the begin End IF
6: ";" No a single statement, mssql there is not.
Code stored procedure, there are several minor problems, oracle-> Comparative -> MSSQL:
. 1: || i.e. link symbol No. +
2: instr function charindex sql functions and functions of the same, but the order of the first two parameters of the inside to turn.
3: substr functions and function substring function sql same.
4: length len function function and function like sql.
5: if ... then ... else end if, mssql there is the begin End IF
6: ";" No a single statement, mssql there is not.
2: Field structure of the database table / view queries:
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: The stored procedure parameter in another table, independent inquiry:
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: Query all tables / views / stored procedures
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 ' ; - all tables
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 ' ; - all tables
Reproduced in: https: //my.oschina.net/secyaher/blog/274086