---
updatedAt: 2025-07-15T09:33:21.000Z
agentTools:
  projectIndex: https://docs.appery.io/llms.txt
---

# Stored Procedure Component

Using the Stored Procedure Component

The **Stored Procedure** component (**SP** component) allows executing Stored Procedure for your database application.

## Connection

In the **Connection** section, select a database connection. If you don't have a connection, then you need to create a [Database Connection](https://docs.appery.io/docs/apiexpress-database-connection).

## Stored Procedure Settings

In the **Stored Procedure Settings** section, select the needed Stored Procedure and provide mapping for Stored Procedure parameters. (If you don't have a ready to use Stored Procedure yet, then you can pick one from our Examples section below.)

Each parameter has a name and type, which you can also map into a variable from the service flow. This means when the service flow is executed, the variables you map will be automatically passed into this component.

## Response Body

The last section of the **SP** component is the **Response body** section. This is where you define the output of this component. You can manually enter the response but we recommend to click the **Generate** button to test the component and set its response this way.

When you click the **Generate** button, a popup will open and there you can test the **Stored Procedure**. You will need to enter any parameter values (if any). Once you have tested the **Stored Procedure**, click **Import response** button to set the response (template) for this component.

## Parameter Types Mapping

API Express works with Relation Database via JDBC driver version 4.0.\
Every database has its own set of data types and all of them have to be mapped into limited set of types:

NUMBER\
STRING\
DATE\
TIME\
TIMESTAMP\
BOOLEAN\
CLOB\
BLOB\
BINARY\
BYTE\
SHORT\
INTEGER\
LONG\
FLOAT\
DOUBLE

> ❗️ In majority cases, API Express defines types of parameters of Stored Procedure automatically but in some cases it is impossible and in this case type of parameter is set as **OTHER** and it is user's responsibility to manually provide a correct type.
>
> Even in the case of automatic type detection, it is **highly recommended** to double check parameter types and change them to correct ones if the detection was wrong.

> 🚧 Pay attention that the **DATE** type contains information only about the year, month and date only. If it's needed to get/pass information about hours, minutes, seconds then the **TIMESTAMP** type should be used.

> ❗️ Some databases provide the ability to create an alias for a data types (for example Boolean type can be an alias on number type) in this case the original type should be specified.

#### Examples section

## MySQL Examples

**MySQL** the example of Stored Procedure which returns passed value

```sql
create procedure echo(IN message varchar(255))
  BEGIN
    SELECT message;
  END;
```

**MySQL** example of Stored Procedure which calculates sqrt of passed parameter

```sql
DELIMITER $$

DROP PROCEDURE IF EXISTS my_sqrt$$
CREATE PROCEDURE my_sqrt(input_number INT, OUT out_number FLOAT)
  BEGIN
    SET out_number=SQRT(input_number);
  END$$

DELIMITER ;
```

Example of working with tables in Stored Procedure

```sql
create table books
(
  id int auto_increment primary key,
  name char(255) not null
)
  engine=InnoDB
;
```

Insert initial data into books table

```sql
insert into books (name) values ('In Search of Lost Time')  ;
insert into books (name) values ('Don Quixote')  ;
insert into books (name) values ('Ulysses')  ;
```

```sql
DELIMITER //
CREATE PROCEDURE GetBooksByName(IN pname VARCHAR(255))
  BEGIN
    SELECT *
    FROM books
    WHERE name = pname;
  END //
DELIMITER ;
```

Procedure INSERT

```sql
DELIMITER //
CREATE PROCEDURE insertBook(IN name VARCHAR(255))
  BEGIN
	insert into books (name) values (name);
  END //
DELIMITER ;
```

API Express response

```json
{
    "data": {
        "updateCount1": 1
    },
    "status": "success"
}
```

Procedure UPDATE

```sql
CREATE PROCEDURE updateBookByName(IN pname VARCHAR(255), IN newName VARCHAR(255))
  BEGIN
  UPDATE books SET name = newName WHERE name = pname;
  END
```

API Express response

```json
{
    "data": {
        "updateCount1": 1
    },
    "status": "success"
}
```

Procedure DELETE

```sql
CREATE PROCEDURE deleteBookByName(IN pname VARCHAR(255))
  BEGIN
  DELETE from books WHERE name = pname;
  END
```

API Express response

```json
{
    "data": {
        "updateCount1": 1
    },
    "status": "success"
}
```

## MSSQL Examples

**MSSQL** example of Stored Procedure which returns passed value

```sql
CREATE PROCEDURE echo(
  @message VARCHAR(255)                   
)
AS
  BEGIN
    SELECT @message as message
  END
```

Invoked Stored Procedure with a value of parameter equal *hello* the response will be:

```json
{
    "data": {
        "#result-set-1": [
            {
                "message": "hello"
            }
        ]
    },
    "status": "success"
}
```

Create books table

```sql
create table books
(
  id int identity primary key,
  name varchar(255)
)
```

Insert initial data

```sql
insert into books (name) values ('In Search of Lost Time')  ;
insert into books (name) values ('Don Quixote')  ;
insert into books (name) values ('Ulysses')  ;
```

Stored Procedure which returns book info by id

```sql
CREATE PROCEDURE GetBookById(
  @id INT
)
AS
  BEGIN
    SELECT * FROM books WHERE id=@id
  END
```

For parameter 1 Stored Procedure component returns

```json
{
    "data": {
        "#result-set-1": [{
            "id": 1,
            "name": "In Search of Lost Time"
        }]
    },
    "status": "success"
}
```

```sql
Create Procedure getBookNameById
    (
      @id as int,
      @name as nvarchar(20) out
    )
  as
    begin
      select @name=name from books where id=@id
    end
```

```json
{
    "data": {
        "@name": "In Search of Lost Ti"
    },
    "status": "success"
}
```

Procedure INSERT

```sql
CREATE PROCEDURE insertBook(
  @name as nvarchar(20)
)
AS
  BEGIN
    insert into books (name) values (@name);
  END
```

API Express response

```json
{
    "data": {
        "updateCount1": 1
    },
    "status": "success"
}
```

Procedure UPDATE

```sql
CREATE PROCEDURE updateBookByName(
  @pname as nvarchar(20),
  @newName as nvarchar(20)
)
AS
  BEGIN
    UPDATE books SET name = @newName WHERE name = @pname;
  END
```

API Express response

```json
{
    "data": {
        "updateCount1": 1
    },
    "status": "success"
}
```

Procedure DELETE

```sql
CREATE PROCEDURE deleteBookByName(@pname as nvarchar(20))
AS
  BEGIN
  DELETE from books WHERE name = @pname;
  END
```

API Express response

```json
{
    "data": {
        "updateCount1": 1
    },
    "status": "success"
}
```

## PostgreSQL Examples

```sql
CREATE OR REPLACE FUNCTION echo(message varchar(255)) RETURNS varchar(255) AS $$
BEGIN
  RETURN message;
END;
$$ LANGUAGE plpgsql;
```

```json
{
    "data": {
        "#result-set-1": [{
            "result": "hello"
        }]
    },
    "status": "success"
}
```

```sql
create table books
(
  id integer not null constraint books_pkey primary key,
  name varchar(255)
)
;
```

```sql
insert into books (id, name) values (1, 'In Search of Lost Time')  ;
insert into books (id, name) values (2, 'Don Quixote')  ;
insert into books (id, name) values (3, 'Ulysses')  ;
```

```sql
CREATE OR REPLACE FUNCTION AddBook(id INT, name varchar(255))
  RETURNS void AS $$
BEGIN
  INSERT INTO books VALUES (id, name);
END;
$$ LANGUAGE plpgsql;
```

```json
{
    "data": {
        "#result-set-1": [
            {
                "result": {
                    "type": "void",
                    "value": ""
                }
            }
        ]
    },
    "status": "success"
}
```

## Oracle Examples

```sql
create table "books"
(
  "id" NUMBER not null primary key,
  "name" VARCHAR2(255)
)
```

Procedure SELECT

```sql
CREATE OR REPLACE PROCEDURE selectBookNameById (id IN number, bookName OUT VARCHAR2)
IS
  BEGIN
    SELECT "name" INTO bookName from "books"  WHERE "id" = id;
  END selectBookNameById;
/
```

API Express response

```json
{
    "data": {
        "BOOKNAME": "Test"
    },
    "status": "success"
}
```

Procedure INSERT

```sql
CREATE OR REPLACE PROCEDURE insertBook (id IN number, pname IN VARCHAR)
as
  BEGIN
    INSERT INTO "books" ("id", "name") VALUES (id, pname);
  END insertBook;
/
```

API Express response

```json
{
    "data": {},
    "status": "success"
}
```

Procedure UPDATE

```sql
CREATE OR REPLACE PROCEDURE updateBook (id IN number, pname IN VARCHAR)
as
  BEGIN
    UPDATE "books" SET "name" = pname WHERE "id" = id;
  END updateBook;
/
```

API Express response

```json
{
    "data": {},
    "status": "success"
}
```

Procedure DELETE

```sql
CREATE OR REPLACE PROCEDURE deleteBook (id IN number)
as
  BEGIN
    DELETE from "books" WHERE "id" = id;
  END deleteBook;
/
```

API Express response

```json
{
    "data": {},
    "status": "success"
}
```