假設當前我們使用一個名為“query”的數據庫,其中包含以下表格 –
mysql> Show tables in query; +-----------------+ | Tables_in_query | +-----------------+ | student_detail | | student_info | +-----------------+ 2 rows in set (0.00 sec)
登錄后復制
現在,以下是一個存儲過程,它將接受數據庫的名稱作為其參數,并為我們提供包含詳細信息的表列表 –
mysql> DELIMITER// mysql> CREATE procedure tb_list(db_name varchar(40)) -> BEGIN -> SET @z := CONCAT('Select * from information_schema.tables WHERE table_schema = ','\'',db_name,'\''); -> Prepare stmt from @z; -> EXECUTE stmt; -> END // Query OK, 0 rows affected (0.06 sec)
登錄后復制
現在通過提供數據庫名稱作為其參數來調用此存儲過程 –
mysql> DELIMITER; mysql> CALL tb_list('query')\G *************************** 1. row *************************** TABLE_CATALOG: def TABLE_SCHEMA: query TABLE_NAME: student_detail TABLE_TYPE: BASE TABLE ENGINE: InnoDB VERSION: 10 ROW_FORMAT: Dynamic TABLE_ROWS: 4 AVG_ROW_LENGTH: 4096 DATA_LENGTH: 16384 MAX_DATA_LENGTH: 0 INDEX_LENGTH: 0 DATA_FREE: 0 AUTO_INCREMENT: NULL CREATE_TIME: 2017-12-13 16:25:44 UPDATE_TIME: NULL CHECK_TIME: NULL TABLE_COLLATION: latin1_swedish_ci CHECKSUM: NULL CREATE_OPTIONS: TABLE_COMMENT: *************************** 2. row *************************** TABLE_CATALOG: def TABLE_SCHEMA: query TABLE_NAME: student_info TABLE_TYPE: BASE TABLE ENGINE: InnoDB VERSION: 10 ROW_FORMAT: Dynamic TABLE_ROWS: 4 AVG_ROW_LENGTH: 4096 DATA_LENGTH: 16384 MAX_DATA_LENGTH: 0 INDEX_LENGTH: 0 DATA_FREE: 0 AUTO_INCREMENT: NULL CREATE_TIME: 2017-12-12 09:52:51 UPDATE_TIME: NULL CHECK_TIME: NULL TABLE_COLLATION: latin1_swedish_ci CHECKSUM: NULL CREATE_OPTIONS: TABLE_COMMENT: 2 rows in set (0.00 sec)
登錄后復制
以上就是創(chuàng)建一個MySQL存儲過程,該存儲過程以數據庫名稱作為參數,列出特定數據庫中包含詳細信息的表。的詳細內容,更多請關注www.92cms.cn其它相關文章!