MySQL5.7: Paging using Mysql Stored Proc

来源:https://www.cnblogs.com/geovindu/archive/2018/09/28/9719605.html
-Advertisement-
Play Games

-- 查詢外鍵 塗聚文 (Geovin Du) select concat(table_name, '.', column_name) as 'foreign key', concat(referenced_table_name, '.', referenced_column_name) as 'r... ...


 

 

-- 查詢外鍵 塗聚文 (Geovin Du)
select
    concat(table_name, '.', column_name) as 'foreign key',  
    concat(referenced_table_name, '.', referenced_column_name) as 'references'
from
    information_schema.key_column_usage
where
    referenced_table_name is not null;

-- 查詢外鍵    
select
    concat(table_name, '.', column_name) as 'foreign key',  
    concat(referenced_table_name, '.', referenced_column_name) as 'references'
from
    information_schema.key_column_usage
where
    referenced_table_name is not null
    and table_schema = 'geovindu';
    
--  table_name  查詢表和視圖
SELECT * FROM information_schema.tables
    WHERE table_schema = 'geovindu';
-- 表    
SELECT * FROM information_schema.tables
    WHERE table_schema = 'geovindu' and table_type='base table';

-- 視圖
SELECT * FROM information_schema.tables
    WHERE table_schema = 'geovindu' and table_type='VIEW';
 -- 列   
 SELECT * FROM information_schema.COLUMNS;
 
 -- 主外鍵
 SELECT * FROM information_schema.KEY_COLUMN_USAGE;
 
 SELECT * FROM information_schema.PARAMETERS;
 
 -- 存儲過程,自定義函數
SELECT * FROM information_schema.PARAMETERS where Specific_schema='geovindu';
-- 'PROCEDURE'
SELECT * FROM information_schema.PARAMETERS where Specific_schema='geovindu' and routine_type='PROCEDURE';
select * from information_schema.ROUTINES where  ROUTINE_SCHEMA='geovindu' and routine_type='PROCEDURE';

--  'FUNCTION'
SELECT * FROM information_schema.PARAMETERS where Specific_schema='geovindu'  and routine_type='FUNCTION';
select * from information_schema.ROUTINES where  ROUTINE_SCHEMA='geovindu' and routine_type='FUNCTION';


  
SELECT * FROM information_schema.PROCESSLIST;
  
 --
 SELECT * FROM information_schema.SCHEMATA;
 
 -- 表,視圖
 SELECT
    TABLE_NAME, ENGINE, VERSION, ROW_FORMAT, TABLE_ROWS, AVG_ROW_LENGTH,
    DATA_LENGTH, MAX_DATA_LENGTH, INDEX_LENGTH, DATA_FREE, AUTO_INCREMENT,
    CREATE_TIME, UPDATE_TIME, CHECK_TIME, TABLE_COLLATION, CHECKSUM,
    CREATE_OPTIONS, TABLE_COMMENT
  FROM INFORMATION_SCHEMA.TABLES
  WHERE table_schema = 'geovindu';
  
    
    
-- 主鍵
select * from information_schema.KEY_COLUMN_USAGE;

-- https://dev.mysql.com/doc/refman/8.0/en/keywords-table.html
select * from information_schema.KEYWORDS;

SELECT * FROM INFORMATION_SCHEMA.KEYWORDS;

select
    concat(table_name, '.', column_name) as 'foreign key',  
    concat(referenced_table_name, '.', referenced_column_name) as 'references'
from
    information_schema.key_column_usage
where
    referenced_table_name is not null;
    
select `column_name`, `column_type`, `column_default`, `column_comment`
from `information_schema`.`COLUMNS` 
where `table_name` = 'customerlist' 
and `table_schema` = 'geovindu';

select *
from `information_schema`.`COLUMNS` 
where `table_name` = 'customerlist' 
and `table_schema` = 'geovindu';   
    
select *
from `information_schema`.`COLUMNS` 
where `table_schema` = 'geovindu';   
-- column_key  PRI,MUL,UNI  pri 主鍵,mul 外鍵
-- EXTRA  auto increment 自動增長
-- DATA_TYPE 數據類型

-- 外鍵表與主表關係
SELECT 
  `TABLE_SCHEMA`,                          -- Foreign key schema
  `TABLE_NAME`,                            -- Foreign key table
  `COLUMN_NAME`,                           -- Foreign key column
  `REFERENCED_TABLE_SCHEMA`,               -- Origin key schema
  `REFERENCED_TABLE_NAME`,                 -- Origin key table
  `REFERENCED_COLUMN_NAME`                 -- Origin key column
FROM
  `INFORMATION_SCHEMA`.`KEY_COLUMN_USAGE`  -- Will fail if user don't have privilege
WHERE
  `TABLE_SCHEMA` = SCHEMA()                -- Detect current schema in USE 
  AND `REFERENCED_TABLE_NAME` IS NOT NULL; -- Only tables with foreign keys
 
 --  
 SELECT * FROM  INFORMATION_SCHEMA.KEY_COLUMN_USAGE  WHERE  TABLE_SCHEMA = 'geovindu'  AND REFERENCED_TABLE_NAME IS NOT NULL; 
  

  
--   
SELECT 
    count(1) totalrelationships ,
    c.table_name tablename,
    CONCAT(' ',GROUP_CONCAT(c.column_name ORDER BY ordinal_position SEPARATOR ', ')) columnname,
    CONCAT(' ',GROUP_CONCAT(c.column_type ORDER BY ordinal_position SEPARATOR ', ')) columntype    
FROM
    information_schema.columns c RIGHT JOIN
    (SELECT column_name , column_type FROM information_schema.columns WHERE 
    -- column_key in ('PRI','MUL') AND  -- uncomment this line if you want to see relations only with indexes
    table_schema = DATABASE() AND table_name = 'productitorderdetails') AS p
    USING (column_name,column_type)
WHERE
    c.table_schema = DATABASE()
    -- AND c.table_name != 'YourTableName'
    GROUP BY tablename
    -- HAVING (locate(' YourColumnName',columnname) > 0) -- uncomment this line to search for specific column 
    ORDER BY totalrelationships desc, columnname
;

-- 
SELECT i.TABLE_SCHEMA, i.TABLE_NAME, 
       i.CONSTRAINT_TYPE, i.CONSTRAINT_NAME, 
       k.COLUMN_NAME, k.REFERENCED_TABLE_NAME, k.REFERENCED_COLUMN_NAME 
  FROM information_schema.TABLE_CONSTRAINTS i 
  LEFT JOIN information_schema.KEY_COLUMN_USAGE k 
       ON i.CONSTRAINT_NAME = k.CONSTRAINT_NAME 
 WHERE i.TABLE_SCHEMA = 'productitorderdetails' AND i.CONSTRAINT_TYPE = 'FOREIGN KEY' 
 ORDER BY i.TABLE_NAME;
 
 --  
 select
    concat(table_name, '.', column_name) as 'foreign key',
    concat(referenced_table_name, '.', referenced_column_name) as 'references',
    constraint_name as 'constraint name'
from
    information_schema.key_column_usage
where
    referenced_table_name is not null
    and table_schema = 'geovindu';
    
 SELECT CONSTRAINT_NAME, TABLE_NAME, REFERENCED_TABLE_NAME
FROM information_schema.REFERENTIAL_CONSTRAINTS
WHERE CONSTRAINT_SCHEMA = 'geovindu'
AND REFERENCED_TABLE_NAME = 'productitorderdetails';

SELECT i.TABLE_SCHEMA, i.TABLE_NAME, i.CONSTRAINT_TYPE, i.CONSTRAINT_NAME, k.REFERENCED_TABLE_NAME, k.REFERENCED_COLUMN_NAME 
FROM information_schema.TABLE_CONSTRAINTS i 
LEFT JOIN information_schema.KEY_COLUMN_USAGE k ON i.CONSTRAINT_NAME = k.CONSTRAINT_NAME 
WHERE i.CONSTRAINT_TYPE = 'FOREIGN KEY'
and i.table_schema = 'geovindu';
 
 SELECT i.TABLE_NAME, i.CONSTRAINT_TYPE, i.CONSTRAINT_NAME, k.REFERENCED_TABLE_NAME, k.REFERENCED_COLUMN_NAME 
FROM information_schema.TABLE_CONSTRAINTS i 
LEFT JOIN information_schema.KEY_COLUMN_USAGE k ON i.CONSTRAINT_NAME = k.CONSTRAINT_NAME 
WHERE i.CONSTRAINT_TYPE = 'FOREIGN KEY' 
AND i.TABLE_SCHEMA = DATABASE()
AND i.TABLE_NAME = 'productitorderdetails';
 
 
 SELECT *
FROM information_schema.REFERENTIAL_CONSTRAINTS;


SELECT 
  TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME
FROM
  INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE
  REFERENCED_TABLE_SCHEMA = 'geovindu' AND
  REFERENCED_TABLE_NAME = 'productitorderdetails';
  
  SELECT 
  TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME
FROM
  INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE
  REFERENCED_TABLE_SCHEMA = 'geovindu' AND
  TABLE_NAME = 'productitorderdetails';
  
  
  
select * 
 from information_schema.TABLES where TABLE_SCHEMA='geovindu';
 
  
  
  -- 主鍵  
 select table_name as 'TableName',column_name as 'FieldName',data_type as 'TypeName',ifnull(character_maximum_length,8) as 'Length',is_nullable as 'IS_NULL' 
 from information_schema.columns where  table_schema='geovindu' and column_key='PRI'; 
 
 -- 主鍵 ,有註釋
  select a.table_name as 'TableName',a.column_name as 'FieldName',a.data_type as 'TypeName',ifnull(a.character_maximum_length,8) as 'Length',a.is_nullable as 'IS_NULL',a.COLUMN_COMMENT,b.TABLE_COMMENT 
 from information_schema.columns as a,information_schema.TABLES as b 
 where  a.table_schema='geovindu' and b.table_schema='geovindu' and column_key='PRI'
 and a.table_name=b.table_name;
  
  
  -- 外鍵 
  select table_name as 'TableName',column_name as 'FieldName',data_type as 'TypeName',ifnull(character_maximum_length,8) as 'Length',is_nullable as 'IS_NULL' 
 from information_schema.columns where  table_schema='geovindu' and column_key='MUL';  
 
  SELECT * FROM  INFORMATION_SCHEMA.KEY_COLUMN_USAGE  WHERE  TABLE_SCHEMA = 'geovindu'  AND REFERENCED_TABLE_NAME IS NOT NULL; 
 
 
 select a.table_name as 'TableName',a.column_name as 'FieldName',a.data_type as 'TypeName',ifnull(a.character_maximum_length,8) as 'Length',a.is_nullable as 'IS_NULL',  b.REFERENCED_TABLE_NAME,b.REFERENCED_COLUMN_NAME
 from information_schema.columns as a,INFORMATION_SCHEMA.KEY_COLUMN_USAGE as b  where a.TABLE_NAME=b.TABLE_NAME and a.table_schema='geovindu' and b.table_schema='geovindu' and a.column_key='MUL'
  AND b.REFERENCED_TABLE_NAME IS NOT NULL; 
  
  -- 自表外鍵  列有註釋
select a.table_name as 'TableName',a.column_name as 'FieldName',a.data_type as 'TypeName',ifnull(a.character_maximum_length,8) as 'Length',a.is_nullable as 'IS_NULL',a.COLUMN_COMMENT,b.REFERENCED_TABLE_NAME,b.REFERENCED_COLUMN_NAME
 from information_schema.columns as a,INFORMATION_SCHEMA.KEY_COLUMN_USAGE as b  where a.table_name=b.TABLE_NAME and a.COLUMN_NAME=b.COLUMN_NAME and a.table_schema='geovindu' and b.table_schema='geovindu' 
 and a.column_key='MUL'
AND b.REFERENCED_TABLE_NAME IS NOT NULL and a.table_name='productorderdetails'; 
  
 -- 主表的主鍵作的外鍵表 列有註釋
select a.table_name as 'TableName',a.column_name as 'FieldName',a.data_type as 'TypeName',ifnull(a.character_maximum_length,8) as 'Length',a.is_nullable as 'IS_NULL',a.COLUMN_COMMENT, b.REFERENCED_TABLE_NAME,b.REFERENCED_COLUMN_NAME
from information_schema.columns as a,INFORMATION_SCHEMA.KEY_COLUMN_USAGE as b  where a.TABLE_NAME=b.TABLE_NAME and a.COLUMN_NAME=b.COLUMN_NAME and a.table_schema='geovindu' and b.table_schema='geovindu' and a.column_key='MUL'
AND b.REFERENCED_TABLE_NAME IS NOT NULL and b.REFERENCED_TABLE_NAME='unitlist'; 
  
 
 -- 表
  select table_name as 'TableName',column_name as 'FieldName',data_type as 'TypeName',ifnull(character_maximum_length,8) as 'Length',is_nullable as 'IS_NULL' 
 from information_schema.columns where  table_schema='geovindu' and column_key='PRI' and table_name=('orderdetails'); 
 
 -- 表
 select column_name as 'FieldName',data_type as 'FieldType',ifnull(character_maximum_length,8) as 'FieldLength' from information_schema.columns where table_schema='geovindu' and table_name=('orderdetails');
 
 --  表,列表有註釋
 select a.column_name as 'FieldName',a.data_type as 'FieldType',ifnull(a.character_maximum_length,8) as 'FieldLength',a.COLUMN_COMMENT,b.TABLE_COMMENT from information_schema.columns as a,information_schema.TABLES as b 
 where a.table_schema='geovindu' and b.table_schema='geovindu' and a.TABLE_NAME=b.TABLE_NAME  and a.table_name=('orderdetails');
 
 
 
 
   select * 
 from information_schema.columns where  table_schema='geovindu' and column_key='PRI' and table_name=('orderdetails'); 
 
  -- UNI
  select table_name as 'TableName',column_name as 'FieldName',data_type as 'TypeName',ifnull(character_maximum_length,8) as 'Length',is_nullable as 'IS_NULL' 
 from information_schema.columns where  table_schema='geovindu' and column_key='UNI';  
 
 -- 查表的描述
 
select  TABLE_COMMENT  from information_schema.TABLES where  table_schema='geovindu' and table_name=('orderdetails'); 
 
 select a.column_name as 'FieldName',a.data_type as 'FieldType',ifnull(a.character_maximum_length,8) as 'FieldLength',a.COLUMN_COMMENT,b.TABLE_COMMENT from information_schema.columns as a,information_schema.TABLES as b  where a.table_schema='geovindu' and b.table_schema='geovindu' and a.TABLE_NAME=b.TABLE_NAME  and a.table_name=('enterprisetype');
 

-- MySQL5.7 2018-09-28 
-- Geovin Du 塗聚文 edit

#查詢函數,存儲過程
SELECT * FROM mysql.proc WHERE db='geovindu';

SELECT * FROM information_schema.routines WHERE routine_schema='geovindu';

SHOW PROCEDURE STATUS WHERE db='geovindu';

#查看存儲過程詳細信息
SHOW CREATE PROCEDURE geovindu.DeleteBookKind;


 -- 存儲過程,自定義函數
SELECT * FROM information_schema.PARAMETERS where Specific_schema='geovindu';
-- 'PROCEDURE'
SELECT * FROM information_schema.PARAMETERS where Specific_schema='geovindu' and routine_type='PROCEDURE';
select * from information_schema.ROUTINES where  ROUTINE_SCHEMA='geovindu' and routine_type='PROCEDURE';

--  'FUNCTION'
SELECT * FROM information_schema.PARAMETERS where Specific_schema='geovindu'  and routine_type='FUNCTION';
select * from information_schema.ROUTINES where  ROUTINE_SCHEMA='geovindu' and routine_type='FUNCTION';



DROP PROCEDURE IF EXISTS `sp_splitpage`;

-- ok
 DELIMITER $$
CREATE PROCEDURE `sp_splitpage`(
 in _pagecurrent int,/*當前頁*/
 in _pagesize int,/*每頁的記錄數*/
 in _ifelse varchar(1000),/*顯示欄位*/
 in _where varchar(1000),/*條件*/
 in _order varchar(1000) /*排序*/
)
COMMENT '分頁存儲過程'
BEGIN
declare strsql varchar(1000);
 if _pagesize<=1 then 
  set _pagesize=20;
end if;
 if _pagecurrent < 1 then 
  set _pagecurrent = 1;
end if;
 set @strsql = concat('select ',_ifelse,' from ',_where,' ',_order,' limit ',_pagecurrent*_pagesize-_pagesize,',',_pagesize); 
 prepare stmtsql from @strsql; 
 execute stmtsql; 
 deallocate prepare stmtsql; 
 set @strsqlcount=concat('select count(1) as count from ',_where);/*count(1) 這個欄位最好是主鍵*/
 prepare stmtsqlcount from @strsqlcount; 
 execute stmtsqlcount; 
 deallocate prepare stmtsqlcount; 
END$$
DELIMITER ;



/*
--名稱:MYSQL版查詢分頁存儲過程 by peace 2013-8-14
--輸入參數:@fields        -- 要查詢的欄位用逗號隔開
--輸入參數:@tables        -- 要查詢的表
--輸入參數:@where        -- 查詢條件
--輸入參數:@orderby    -- 排序欄位
--輸出參數:@page        -- 當前頁計數從1開始
--輸出參數:@pagesize    -- 每頁大小
--輸出參數:@totalcount -- 總記錄數
--輸出參數:@pagecount  -- 總頁數 
*/
-- ok
DROP PROCEDURE IF EXISTS `Query_Pagination`; 

 DELIMITER $$ 
CREATE PROCEDURE Query_Pagination
(
    in _fields varchar(2000),   
    in _tables text, 
    in _where varchar(2000),  
    in _orderby varchar(200),
    in _pageindex int,
    in _pagesize int,
    in _sumfields  varchar(200),/*增加統計欄位2013-5-8 peaceli*/
    out _totalcount int ,
    out _pagecount int 
)COMMENT '分頁存儲過程'
begin
declare startRow int;
declare pageSize int;
declare rowindex int;
declare strsql varchar(1000);

   set startRow = _pagesize*(_pageindex-1);
   set pageSize = _pagesize;  
   set rowindex = 0;
     set strsql = CONCAT('select sql_calc_found_rows @rowindex:=@rowindex+1 as rownumber,',_fields,' from ',_tables,case ifnull(_where,'') when '' then '' else concat(' where ',_where) end,' order by ',_orderby,' limit ',@startRow,',',@pageSize);
     prepare strsql from @strsql;
     execute strsql;
   deallocate prepare strsql;
   set _totalcount = found_rows(); 

   if(_totalcount <= _pagesize) then
		set _pagecount = 1;
   else if(_totalcount % _pagesize > 0) then
		set _pagecount = _totalcount / _pagesize + 1;
   else
        set _pagecount = _totalcount / _pagesize;
   end if;

if(ifnull(_sumfields,'') <> '') then 
set @sumsql = contact('select ',_sumfields,' from ',_tables,case ifnull(_where,'') when '' then '' else concat(' where ',_where) end); 
prepare sumsql from @sumsql; 
execute sumsql; 
deallocate prepare sumsql; 
end if;
end if; 
end$$
DELIMITER ;





 
/*test"
CALL sp_viewPage( 
'*'#查詢欄位 
,'userupdatelog'#表名 
,'1=1'#條件 
,'Id desc'#排序 
,1 #頁碼 
,20 #每頁記錄數 
,@totalcount #輸出總記錄數 
,@pagecount #輸出用頁數 
); 
SELECT @totalcount,@pagecount; 
*/
DROP PROCEDURE IF EXISTS `sp_viewPage`;
-- OK
 DELIMITER $$
CREATE PROCEDURE sp_viewPage(
_fields VARCHAR(1000), #要查詢的欄位,用逗號(,)分隔 
_tables TEXT, #要查詢的表 
_where VARCHAR(2000), #查詢條件 
_orderby VARCHAR(200), #排序規則 
_pageindex INT, #查詢頁碼 
_pageSize INT, #每頁記錄數 
/*_sumfields VARCHAR(200),#求和欄位 */
#輸出參數 
OUT _totalcount INT, #總記錄數 
OUT _pagecount INT #總頁數 
/* OUT _sumResult VARCHAR(2000)#求和結果 */
)COMMENT '分頁存儲過程'
BEGIN
#140529-xxj-分頁存儲過程 
#計算起始行號
declare strsql varchar(1000);
declare startRow int;
declare pageSize int;
declare rowindex int;
SET startRow = _pageSize * (_pageindex - 1); 
SET pageSize = _pageSize; 
SET rowindex = 0; #行號 

#合併字元串 
SET @strsql = CONCAT( 
#'select sql_calc_found_rows @rowindex:=@rowindex+1 as rownumber,' #記錄行號 
'select sql_calc_found_rows ' 
,_fields 
,' from ' 
,_tables 
,CASE IFNULL(_where, '') WHEN '' THEN '' ELSE CONCAT(' where ', _where) END 
,CASE IFNULL(_orderby, '') WHEN '' THEN '' ELSE CONCAT(' order by ', _orderby) END 
,' limit ' 
,startRow 
,',' 
,pageSize 
); 

PREPARE strsql FROM @strsql;#定義預處理語句 
EXECUTE strsql; #執行預處理語句 
DEALLOCATE PREPARE strsql; #刪除定義 
#通過 sql_calc_found_rows 記錄沒有使用 limit 語句的記錄,使用 found_rows() 獲取行數 
SET _totalcount = FOUND_ROWS(); 
#計算總頁數 
IF (_totalcount <= _pageSize) THEN 
	SET _pagecount = 1;
ELSE IF (_totalcount % _pageSize > 0) THEN 
	SET _pagecount = _totalcount DIV _pageSize + 1; 
ELSE 
SET _pagecount = _totalcount DIV _pageSize; 
END IF; 
end if;

END$$
DELIMITER ;

-- Ok
DROP PROCEDURE IF EXISTS `GetRecordAsPage`;

DELIMITER $$
CREATE PROCEDURE `GetRecordAsPage` 
(in tbName varchar(800),  -- 表名
in fldName varchar(1000), -- 表的列名
in strWhere varchar(500),  -- 查詢條件
in pageIndex int, -- 第幾頁 傳入1就是顯示第一頁
in pageSize int,  -- 一頁顯示幾條記錄
in orderType int, -- 0是升序 非0是降序
in sortName varchar(50) -- 排序欄位
)
COMMENT '分頁存儲過程'
BEGIN
declare startRow int;
declare sqlStr varchar(1000);
declare limitTemp varchar(1000);
declare orderTemp varchar(1000); 
set startRow = (pageIndex-1)*pageSize; 
set sqlStr = CONCAT('SELECT ',fldName,' from ',tbName);
set limitTemp = CONCAT(' limit ',startRow,',',pageSize);
set orderTemp = CONCAT(' order by ',sortName);
if orderType = 0 then
set orderTemp = CONCAT(orderTemp,' ASC ');
else
set orderTemp = CONCAT(orderTemp,' DESC ');
end if; 
set @sqlString = CONCAT(sqlStr,' ',strWhere,orderTemp,limitTemp); 
prepare sqlstmt from @sqlString;
execute sqlstmt;
deallocate prepare sqlstmt; 
END$$ 
DELIMITER ;

-- 
DELIMITER $$
DROP PROCEDURE IF EXISTS `GetRecordCount` $$
-- --CREATE DEFINER=`root`@`localhost` PROCEDURE `GetRecordCount`(in tbName varchar(800),in strWhere varchar(500))
CREATE  PROCEDURE `GetRecordCount`
(
in tbName varchar(800),
in strWhere varchar(500)
)COMMENT '獲取條件下的總記錄數據 存儲過程'
BEGIN
set @strSQL=concat('select count(*) as countStr from ',tbName,strWhere);
prepare sqlstmt from @strSQL;
execute sqlstmt;
deallocate prepare sqlstmt;
END $$
DELIMITER ;

-- OK
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `LazyLoadScope`
(
IN ClientId    INT,
IN StartIndex INT,
IN Count INT
)COMMENT '分頁存儲過程'
BEGIN
DECLARE LowerBound INT;
DECLARE UpperBound INT;
DECLARE rownum INT;
SET LowerBound = ((StartIndex - 1) * Count) + 1;
SET UpperBound = ((StartIndex - 1) * Count) + Count;

SELECT scopeid,scopename,clientid,scope,createddate,ViewDate,IsLocked
  from (SELECT *, @rownum := @rownum + 1 AS rank 
  from (SELECT   sm.scopeid,sm.scopename,sm.clientid,sm.scope,sm.createddate,sm.ViewDate,sm.Is     Locked
FROM scopemaster as sm
inner join clientmaster cm on cm.clientid=sm.clientid
where cm.userid=ClientId order by sm.ViewDate desc) d, (SELECT @rownum  := 0) r ) m
WHERE rank >= LowerBound and rank <= UpperBound;

END$$
DELIMITER ;




DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetCustomers_Pager`(
   _PageIndex INT
   ,_PageSize INT
   ,OUT _RecordCount INT
)COMMENT '分頁存儲過程'
BEGIN
       SET @RowNumber:=0;
 
       CREATE TEMPORARY TABLE Results
       SELECT @RowNumber:=@RowNumber+1 RowNumber
              ,CustomerID
              ,ContactName
              ,CompanyName
       FROM Customers;
 
       SET _RecordCount =(SELECT COUNT(*) FROM Results);
 
       SELECT * FROM Results
       WHERE RowNumber BETWEEN(_PageIndex -1) * _PageSize + 1 AND(((_PageIndex -1) * _PageSize + 1) + _PageSize) - 1;
 
       DROP TEMPORARY TABLE Results;
END$$
DELIMITER ;

-- OK
 DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE ProcPage(
in tableName varchar(20), #表名
in showField varchar(100), #要顯示的列名
in whereText varchar(500), #where條件(只需要寫where後面的語句)
in orderText varchar(500), #排序條件(只需要寫order by後面的語句)
in pageSize int, #每一頁顯示的記錄數
in pageIndex int, #當前頁
out dataCount int  #總記錄數
)COMMENT '分頁存儲過程'
BEGIN
DECLARE f INT unsigned DEFAULT 0;  
set f=1;
if _pagesize<=100 then
  set f=200;
end if;
if(pageSzie<1) then
 set pagesize=20;
end if;
if(pageIdex<1) then
  set pageIndex=1;
end if;
if(length(whereText)>0) then
 set whereText=concat(' where 1=1 ',whereText);
end if; 
if(LENGTH(orderText)>0)then
	set orderText = CONCAT(' ORDER BY ',orderText);
end if; 
 /*
if (pageSize<1) then
set pageSize=20;
end if; 
if (pageIndex < 1)then
  set pageIndex = 1;
end if; 
if(LENGTH(whereText)>0)then
	set whereText=CONCAT(' where 1=1 ',whereText);
end if; 
*/
set @strsql = CONCAT('select ',showField,' from ',tableName,' ',whereText,' ',orderText,' limit ',pageIndex*pageSize-pageSize,',',pageSize); 
prepare stmtsql from @strsql;
execute stmtsql;
deallocate prepare stmtsql; 
set @strsqlcount=concat('select count(1) as count into @datacount from ',tableName,'',whereText);
prepare stmtsqlcount from @strsqlcount;
execute stmtsqlcount;
deallocate prepare stmtsqlcount;
set datacount=@datacount;
END$$
DELIMITER ;

  


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 參考資料: https://docs.konghq.com/install/source/ 環境準備:操作系統 centeros7.3 1 :openssl和pcre一般系統自帶,如果沒有可自己安裝 https://www.openssl.org/source/ 或者 yum -y install ...
  • 摘要: 今天對物理數據表,進行增加列操作後,程式一直顯示無法找到相應列,通過仔細比對發現,視圖中無相應列更新,下文將具體的解決方法分享如下: 例: 轉自:http://www.maomao365.com/?p=7471 ...
  • 在拉薩藏民的客棧里,氣溫18度,碧藍的天空里大朵大朵飽滿的雲朵,耳機里放的陳鴻宇的民謠,下午五點正值陽光最佳,暖和和的氛圍里,有點小高反,所以這次,不寫技術博文,想寫下今年的一些人與事,刪減部分,留下職場部分。 如果轉載,請註明博文來源: www.cnblogs.com/xinysu/ ,版權歸 博 ...
  • [20180926]神奇的規避ORA-01795方法.txt--//大家知道in裡面的值限制1000個值,如果超出報ORA-01795錯誤.D:\> ooerr 0179501795, 00000, "maximum number of expressions in a list is 1000"D ...
  • 深入理解redis數據類型:String,Lists,Sets,Sorted Sets,Hashes,Bitmaps,HyperLogLogs等 ...
  • 1、如device表中的identity欄位正常的欄位長度是32位,但是某些不正常的數據,後面多出空格,需要去掉後面的空格,可執行以下命令: 2、使用substring函數截取某欄位的的其中一部分的方法,如截取identity第21位及其之後的數據,並保存位identity12欄位: ...
  • 這篇博客是在學習MySQL Server 5.7版的源代碼過程中編寫的第一個學習筆記,主要內容包括: 連接處理總體過程 連接監聽總體過程 登錄驗證 命令交互總體過程 命令的種類 命令的執行結果 支持的協議以及相關的類 連接處理總體過程 在一個線程中來處理與一個mysql客戶端程式的整個會話過程中的全 ...
  • SQL Server中通用資料庫角色許可權處理 最近和同事在做資料庫許可權清理的事情,主要是刪除一些賬號;取消一些賬號的較大的許可權等,例如,有一些有db_owner許可權,我們取消賬號的資料庫角色db_owner,授予最低要求的相關許可權。但是這種工作完全是一個體力活,而且是吃力不討好,而且推進很慢。另外,... ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...