What is the default parameter mode in mysql?


Parameters make the stored procedure more useful and flexible. In MySQL, we have the following three kinds of modes −

IN mode

It is the default mode. When we define an IN parameter in a stored procedure, the calling program has to pass an argument to the stored procedure. The value of an IN parameter is protected which means that even the value of the IN parameter is changed inside the stored procedure; its original value is retained after the stored procedure ends.

OUT mode

The value of an OUT parameter can be changed inside the stored procedure and its new value is passed back to the calling program. It is to be noticed that the stored procedure cannot access the initial value of the OUT parameter when it starts.

INOUT mode

An INOUT parameter is the combination of IN and OUT parameters which means that the calling program may pass the argument, and the stored procedure can modify the INOUT parameter and pass the new value back to the calling program.

Syntax for defining a parameter

Following is the syntax of defining a parameter in the stored procedure −

MODE parameter_name parameter_type(parameter_size)

Here, MODE can be IN, OUT or INOUT which depends upon the purpose of the parameter in the stored purpose.

  • Parameter_name is the name of the parameter.
  • Parameter_type is the data type of parameter.
  • Parameter_size is the size of the parameter

What is the default parameter mode in mysql?

Updated on 22-Jun-2020 05:29:17

  • Related Questions & Answers
  • What are the different time format characters used by MySQL DATE_FORMAT() function?
  • What are the different commands used in MySQL?
  • What do you mean by Scope of variables inside MySQL stored procedure?
  • What are different date format characters used by MySQL DATE_FORMAT() function?
  • Display description of MySQL stored procedure
  • What are the prerequisites for starting writing and using MySQL stored procedure?
  • How can local variables be used in MySQL stored procedure?
  • How can user variables be used in MySQL stored procedure?
  • How Can MySQL CASE statement be used in stored procedure?
  • What are the different compilation modes of a module in Java 9?
  • MySQL stored procedure parameters don't seem to work with special character @?
  • What are different methods of passing parameters in C#?
  • How can a MySQL stored procedure call another MySQL stored procedure inside it?
  • How MySQL IF statement can be used in a stored procedure?
  • How MySQL WHILE loop statement can be used in stored procedure?

Summary: in this tutorial, you will learn how to create stored procedures with parameters, including IN, OUT, and INTOUT parameters.

Introduction to MySQL stored procedure parameters

Often, stored procedures have parameters. The parameters make the stored procedure more useful and reusable. A parameter in a stored procedure has one of three modes: IN,OUT, or INOUT.

IN parameters

IN is the default mode. When you define an IN parameter in a stored procedure, the calling program has to pass an argument to the stored procedure.

In addition, the value of an IN parameter is protected. It means that even you change the value of the IN parameter inside the stored procedure, its original value is unchanged after the stored procedure ends. In other words, the stored procedure only works on the copy of the IN parameter.

OUT parameters

The value of an OUT parameter can be changed inside the stored procedure and its new value is passed back to the calling program.

Notice that the stored procedure cannot access the initial value of the OUT parameter when it starts.

INOUT parameters

An INOUT  parameter is a combination of IN and OUT parameters. It means that the calling program may pass the argument, and the stored procedure can modify the INOUT parameter, and pass the new value back to the calling program.

Defining a parameter

Here is the basic syntax of defining a parameter in stored procedures:

[IN | OUT | INOUT] parameter_name datatype[(length)]

Code language: SQL (Structured Query Language) (sql)

In this syntax,

  • First, specify the parameter mode, which can be IN , OUT or INOUT depending on the purpose of the parameter in the stored procedure.
  • Second, specify the name of the parameter. The parameter name must follow the naming rules of the column name in MySQL.
  • Third, specify the data type and maximum length of the parameter.

MySQL stored procedure parameter examples

Let’s take some examples of using stored procedure parameters.

The IN parameter example

The following example creates a stored procedure that finds all offices that locate in a country specified by the input parameter countryName:

DELIMITER // CREATE PROCEDURE GetOfficeByCountry( IN countryName VARCHAR(255) ) BEGIN SELECT * FROM offices WHERE country = countryName; END // DELIMITER ;

Code language: SQL (Structured Query Language) (sql)

In this example, the countryName is the IN parameter of the stored procedure.

Suppose that you want to find offices locating in the USA, you need to pass an argument (USA) to the stored procedure as shown in the following query:

CALL GetOfficeByCountry('USA');

Code language: SQL (Structured Query Language) (sql)
What is the default parameter mode in mysql?

To find offices in France, you pass the literal string France to the GetOfficeByCountry stored procedure as follows:

CALL GetOfficeByCountry('France')

Code language: SQL (Structured Query Language) (sql)
What is the default parameter mode in mysql?

Because the countryName is the IN parameter, you must pass an argument. If you don’t do so, you’ll get an error:

CALL GetOfficeByCountry();

Code language: SQL (Structured Query Language) (sql)

Here’s the error:

Error Code: 1318. Incorrect number of arguments for PROCEDURE classicmodels.GetOfficeByCountry; expected 1, got 0

Code language: JavaScript (javascript)

The OUT parameter example

The following stored procedure returns the number of orders by order status.

DELIMITER $$ CREATE PROCEDURE GetOrderCountByStatus ( IN orderStatus VARCHAR(25), OUT total INT ) BEGIN SELECT COUNT(orderNumber) INTO total FROM orders WHERE status = orderStatus; END$$ DELIMITER ;

Code language: SQL (Structured Query Language) (sql)

The stored procedure GetOrderCountByStatus() has two parameters:

  • The orderStatus: is the IN parameter specifies the status of orders to return.
  • The total: is the OUT parameter that stores the number of orders in a specific status.

To find the number of orders that already shipped, you call GetOrderCountByStatus and pass the order status as of Shipped, and also pass a session variable ( @total ) to receive the return value.

CALL GetOrderCountByStatus('Shipped',@total); SELECT @total;

Code language: SQL (Structured Query Language) (sql)
What is the default parameter mode in mysql?

To get the number of orders that are in-process, you call the stored procedure GetOrderCountByStatus as follows:

CALL GetOrderCountByStatus('in process',@total); SELECT @total AS total_in_process;

Code language: SQL (Structured Query Language) (sql)
What is the default parameter mode in mysql?

The INOUT parameter example

The following example demonstrates how to use an INOUT parameter in a stored procedure:

DELIMITER $$ CREATE PROCEDURE SetCounter( INOUT counter INT, IN inc INT ) BEGIN SET counter = counter + inc; END$$ DELIMITER ;

Code language: SQL (Structured Query Language) (sql)

In this example, the stored procedure SetCounter() accepts one INOUT parameter ( counter ) and one IN parameter ( inc ). It increases the counter ( counter ) by the value of specified by the inc parameter.

These statements illustrate how to call the SetSounter stored procedure:

SET @counter = 1; CALL SetCounter(@counter,1); -- 2 CALL SetCounter(@counter,1); -- 3 CALL SetCounter(@counter,5); -- 8 SELECT @counter; -- 8

Code language: SQL (Structured Query Language) (sql)

Here is the output:

What is the default parameter mode in mysql?

In this tutorial, you have learned how create stored procedures with parameters including IN, OUT, and INOUT parameters.

Was this tutorial helpful?

What is the default mode of parameter?

IN mode. It is the default mode. When we define an IN parameter in a stored procedure, the calling program has to pass an argument to the stored procedure.

What are 3 modes of parameters?

PL/SQL procedure parameters can have one of three possible modes: IN, OUT, or IN OUT.

What is parameter in MySQL?

In general, a parameter is a placeholder for a variable that contains some value of some type when executing a general-purpose query, or arguments and return values when a stored procedure is executed. Parameter is represented by MySql.

What is default parameter in stored procedure?

The default is an input parameter. To specify an output parameter, the OUTPUT keyword must be specified in the definition of the parameter in the CREATE PROCEDURE statement. The procedure returns the current value of the output parameter to the calling program when the procedure exits.