Oracle 데이터베이스 스토리지 프로세스

1、Oracle      
CREATE OR REPLACE PROCEDURE proc_page
(
       p_tblName IN VARCHAR,
       p_fields IN VARCHAR,
       p_order IN VARCHAR,
       p_pageSize IN INT := 10,
       p_pageIndex IN INT DEFAULT 1,
       p_rowCount OUT INT,
       p_returnDesc OUT VARCHAR,
       p_sel_cur OUT sys_refcursor   /* oracle    mssql                      */
)
AS
       /*AS   Begin                     */
       v_sqlCount VARCHAR(200);
       v_sql VARCHAR(300);
       v_fields VARCHAR(300);
BEGIN
       IF(length(trim(p_tblName)) = 0) THEN
               p_returnDesc := '      !';
               RETURN;  
       END IF;


       v_sqlCount := 'select count(*) from '||p_tblName;
       /**    sql                */
       execute immediate v_sqlCount INTO p_rowCount;


       /**      */
       IF(TRIM(p_fields)='*') Then
                v_fields := p_tblName||'.'||p_fields;
       END IF;
       IF(TRIM(p_fields)!='*') Then
                v_fields := p_fields;
       END IF;


       v_sql := 'select * from (select '||v_fields||',rownum rn from '||p_tblName||' where rownum <= ';
       v_sql := v_sql || (p_pageIndex*p_pageSize) || ' ) tmp where tmp.rn > ';
       v_sql := v_sql || (p_pageIndex-1)*p_pageSize;
       DBMS_OUTPUT.put_line(v_sql);
       OPEN p_sel_cur FOR v_sql;  /**          */
END;
2、MySQl      ----(  Oracle)
drop procedure if exists proc_page;
/**       ,       ,         $$*/
delimiter $$
CREATE PROCEDURE proc_page
(
p_tblName VARCHAR(255),
p_fields VARCHAR(255),
p_order VARCHAR(255),
p_pageSize INT,
p_pageIndex INT,
p_where VARCHAR(300),
OUT p_returnDesc VARCHAR(500),
OUT p_rowCount INT
)
BEGIN
      /**     */
IF(LENGTH(LTRIM(RTRIM(p_tblName))) = 0) THEN
  SET p_returnDesc :=  '     !';
END IF;
/**      */
  SET @count_sql  := concat('select count(*)  into @t_rowcount  from ',p_tblName);
IF(LENGTH(LTRIM(RTRIM(p_where))) > 0) THEN
  SET @count_sql := concat(@count_sql,'  where  ',p_where);
END IF;
/**      */
PREPARE stmt_count From @count_sql;
EXECUTE stmt_count;
DEALLOCATE PREPARE stmt_count;
/**             */
SET p_rowCount := @t_rowcount;
/**      */
SET @sql := concat('select  ',p_fields,'  from ',p_tblName);
IF(LENGTH(LTRIM(RTRIM(p_where))) > 0) THEN
  SET @sql := concat(@sql,'  where  ',p_where);
END IF;
SET @sql := concat(@sql,' limit ',(p_pageIndex-1)*p_pageSize,' , ',p_pageSize);


PREPARE stmt_page FROM @sql;
execute stmt_page;
DEALLOCATE PREPARE stmt_page;
END;
$$
delimiter ;
 

좋은 웹페이지 즐겨찾기