Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing optional parameters within stored procedures in MySQL? [duplicate]

I would like to create a stored procedure which updates either all fields in a table or just a few of them according to parameters passed to it.

How do I create a stored procedure that accepts optional parameters?

like image 725
user1573747 Avatar asked Sep 29 '12 11:09

user1573747


People also ask

Can a stored procedure have optional parameters?

How to create stored procedure that accepts optional parameter in SQL Server? To create optional parameter in stored procedure, we set the parameter value to NULL while creating a stored procedure.

Can pass 3 types of parameters to stored procedures What are they?

As a program, a stored procedure can take parameters. There are three types of parameters: IN, OUT and INOUT.

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.

What is optional parameter in stored procedure?

A parameter is considered optional if the parameter has a default value specified when it is declared. It is not necessary to provide a value for an optional parameter in a procedure call. The default value of a parameter is used when: No value for the parameter is specified in the procedure call.


1 Answers

Optional Parameters are not yet supported on MySQL. I'm suggesting that you pass null value in your parameter and inside your stored procedure has an IF statement.

DELIMITER $$ CREATE PROCEDURE procName (IN param VARCHAR(25)) BEGIN    IF param IS NULL THEN        -- statements ;    ELSE commands       -- statements ;    END IF; END$$ DELIMITER ; 
like image 144
John Woo Avatar answered Oct 21 '22 11:10

John Woo