In PL/SQL you can reference the type of a table column for procedure variables and arguments using the TABLE.COLUMN_NAME%type syntax. This makes code maintenance much easier. Is there an equivalent of this in the mysql stored procedure language?
Where might one need this? Consider the following insert statement:
INSERT INTO NL_LIST (NAME, EMAIL)
SELECT
NAME,
normalizeEmail(EMAIL)
FROM
RAW_NL_LIST;
For this i would like to be able to define normalizeEmail in(roughly) the following way:
CREATE FUNCTION normalizeEmail(email RAW_NL_LIST.EMAIL%type)
RETURNS NL_LIST.EMAIL%type
BEGIN
... implementation here
END;
You can get the MySQL table columns data type with the help of “information_schema. columns”. SELECT DATA_TYPE from INFORMATION_SCHEMA. COLUMNS where table_schema = 'yourDatabaseName' and table_name = 'yourTableName'.
Example: Calling a stored procedure with parametersmysql> CREATE TABLE Emp (Name VARCHAR(255), Salary INT, Location VARCHAR(255)); Assume we have created a stored procedure InsertData which accepts the name, salary and location values and inserts them as a record into the above create (Emp) table.
No, you cannot automatically match the type used in the table definition with the type in a stored procedure. You will have to lookup the table definition yourself and enter the correct type.
See: http://dev.mysql.com/doc/refman/5.6/en/create-procedure.html
Here it says:
CREATE
[DEFINER = { user | CURRENT_USER }]
PROCEDURE sp_name ([proc_parameter[,...]])
[characteristic ...] routine_body
CREATE
[DEFINER = { user | CURRENT_USER }]
FUNCTION sp_name ([func_parameter[,...]])
RETURNS type
[characteristic ...] routine_body
proc_parameter:
[ IN | OUT | INOUT ] param_name type
func_parameter:
param_name type
type:
Any valid MySQL data type <<<-- no special magic allowed
....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With