Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql Stored Procedures Column Type Reference

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;
like image 294
jens_profile Avatar asked Jan 26 '11 16:01

jens_profile


People also ask

How do I find the datatype of a column in SQL?

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'.

How do you call a stored procedure with parameters in MySQL?

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.


1 Answers

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

....
like image 96
Johan Avatar answered Sep 22 '22 08:09

Johan