MySQL資料庫帶INOUT參數執行搜索提示的存儲過程

MySQL帶INOUT參數執行搜尋提示的存儲過程

 

[sql] 
DELIMITER $$  
  
USE `b10k`$$  
  
DROP PROCEDURE IF EXISTS `sp_suggest`$$  
  
CREATE DEFINER=`luth`@`%` PROCEDURE `sp_suggest`(  
    IN  query_column    VARCHAR(100),   /*被檢索的字段名*/  
    IN      keyword     VARCHAR(100),   /*檢索的關鍵字*/  
    IN  table_name  VARCHAR(100),   /*被檢索的表名*/  
    INOUT   result_count    INT     /*要取出匹配記錄的數量*/  
    )  
COMMENT '執行關鍵字搜尋'  
BEGIN  
    /*設定變量*/  
    DECLARE m_begin_row INT DEFAULT 0;  
    DECLARE m_where_string CHAR(128);  
    DECLARE m_order_string CHAR(128);  
    DECLARE m_limit_string CHAR(64);  
  
      
    /*構造語句*/      
    SET m_begin_row = result_count;  
    SET m_where_string = CONCAT(' WHERE ', query_column, ' LIKE \'', keyword, '%\' ');  
    SET m_order_string = CONCAT(' ORDER BY ', query_column);  
    SET m_limit_string = CONCAT(' LIMIT ', result_count);  
      
      
    SET @COUNT_STRING = CONCAT('SELECT DISTINCT COUNT(*) INTO @ROWS_TOTAL FROM ', table_name, ' ', m_where_string, ' ', m_order_string, ' ', m_limit_string);  
    SET @MAIN_STRING = CONCAT('SELECT DISTINCT ', query_column, ' FROM ', table_name, ' ', m_where_string, ' ', m_order_string, ' ', m_limit_string);  
      
    /*預處理*/  
    PREPARE count_stmt FROM @COUNT_STRING;  
    EXECUTE count_stmt;  
    DEALLOCATE PREPARE count_stmt;  
    SET result_count = @ROWS_TOTAL;      
      
    PREPARE main_stmt FROM @MAIN_STRING;  
    EXECUTE main_stmt;  
    DEALLOCATE PREPARE main_stmt;  
END$$  
  
DELIMITER ;  
  
/*調用*/  
SET @aa=10;  
CALL sp_suggest('latin_name','A','species',@aa);  
SELECT @aa;  

 

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。