執行 SHOW CREATE TABLE 的 MySQL 儲存過程?


若要在儲存過程中執行 SHOW CREATE TABLE,使用 SHOW CREATE TABLE。我們首先建立一個表 -

mysql> create table DemoTable2011
   -> (
   -> StudentId int NOT NULL AUTO_INCREMENT,
   -> StudentName varchar(20),
   -> StudentAge int,
   -> StudentCountryName varchar(20),
   -> PRIMARY KEY(StudentId)
   -> );
Query OK, 0 rows affected (0.80 sec)

下面是執行 SHOW CREATE TABLE 的儲存過程 -

mysql> delimiter //
mysql> create procedure test_show_create_demo(table_name varchar(100))
   -> begin
   -> set @query=concat("SHOW CREATE TABLE ",table_name);
   -> prepare st from @query;
   -> execute st;
   -> end
   -> //
Query OK, 0 rows affected (0.22 sec)

mysql> delimiter ;

使用 CALL 命令呼叫儲存過程 -

mysql> call test_show_create_demo('DemoTable2011');

這將產生以下輸出 -

+---------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table
|
+---------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| DemoTable2011 | CREATE TABLE `demotable2011` (
   `StudentId` int(11) NOT NULL AUTO_INCREMENT,
   `StudentName` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
   `StudentAge` int(11) DEFAULT NULL,
   `StudentCountryName` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
   PRIMARY KEY (`StudentId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |
+---------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)

Query OK, 0 rows affected, 1 warning (0.06 sec)

更新日期: 06-04-2020

402 瀏覽量

啟動您的職業生涯

完成課程後獲得認證

立即開始
廣告
© . All rights reserved.