學習來源: CodeData - MySQL 超新手入門(12)Prepared Statement
SELECT Code, Name, GNP
FROM country
WHERE Code = 'USA'
SELECT Code, Name, GNP
FROM country
WHERE Code = 'JPN'
PREPARE my_country
FROM
'SELECT Code, Name, GNP FROM country
WHERE Code = ?'
SET @my_code = 'USA'
EXECUTE my_country USING @my_code
SET @my_code = 'JPN'
EXECUTE my_country USING @my_code
使用者變數儲存一些簡單的資料,例如數字或字串,它們可以在後續的操作中使用。語法:
sql SET @變數名稱1 { = | := } 值 [, @變數名稱2 { = | := } 值, ...]
SELECT @max_gnp := MAX(GNP),
@max_population := MAX(Population)
FROM country
PREPARE my_country
FROM
'SELECT Code, Name, GNP FROM country
WHERE Code = ?';
SET @my_code = 'JPN'
EXECUTE my_country USING @my_code;
{DEALLOCATE | DROP } PREPARE 名稱
PREPARED new_dept FROM
'INSERT INTO cmdev.dept VALUES(?,?,?)';
EXECUTE new_dept USING 參數一, 參數二, 參數三;
PREPARED world.my_city FROM 'SELECT * FROM city';