# Support for postgresql \>= 11.1 stored procedures?

**URL:** https://discourse.diesel.rs/t/support-for-postgresql-11-1-stored-procedures/99
**Category:** Uncategorized
**Created:** [July 1, 2019, 4:33pm UTC](https://discourse.diesel.rs/t/support-for-postgresql-11-1-stored-procedures/99 "2019-07-01T16:33:01Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![hrbigelow](https://avatars.discourse-cdn.com/v4/letter/h/eb9ed0/32.png) [@hrbigelow](https://discourse.diesel.rs/u/hrbigelow)
#### Post date: [July 1, 2019, 4:33pm UTC](https://discourse.diesel.rs/t/support-for-postgresql-11-1-stored-procedures/99/1 "2019-07-01T16:33:01Z")

</div>

I wondered (perhaps using `QueryBuilder`?) if it is currently possible for Diesel to issue:

call `my_stored_proc(in_par1, in_par2, out_par1)`

where `my_stored_proc` is created as:

```auto
create procedure my_stored_proc(
	name in varchar,
	description in varchar,
	error_msg out varchar)
as
$$
declare
	...
begin
        ...
end;
$$ language pgplsql;

```

In particular, to be able to bind both in and out parameters, and to be able to collect database exceptions that are RAISE’ed inside the procedure.

It would be great to have this in order to leverage both the power of stored procedures, while still having the flexibility of Diesel’s ORM.

---

<div class="post-metadata">

### Author: ![sean](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.diesel.rs/sean/32/2_2.png) [@sean](https://discourse.diesel.rs/u/sean)
#### Post date: [July 2, 2019, 4:46pm UTC](https://discourse.diesel.rs/t/support-for-postgresql-11-1-stored-procedures/99/2 "2019-07-02T16:46:05Z")

</div>

We don’t currently have any built-in support for stored procedures, but you could implement your own `QueryFragment` for the `CALL` keyword if you wanted. `sql_function!` would be fine for stored procedure declaration.

That said, it doesn’t sound like you actually need to be using stored procedures at all. The only difference between a procedure and a function is the ability to manage transaction state. If you’re not using `BEGIN`/`COMMIT`/`ROLLBACK` in the body, there’s no reason it can’t be done with a function returning `void`

---

<div class="post-metadata">

### Author: ![hrbigelow](https://avatars.discourse-cdn.com/v4/letter/h/eb9ed0/32.png) [@hrbigelow](https://discourse.diesel.rs/u/hrbigelow)
#### Post date: [July 2, 2019, 5:15pm UTC](https://discourse.diesel.rs/t/support-for-postgresql-11-1-stored-procedures/99/3 "2019-07-02T17:15:50Z")

</div>

Hi Sean,

Actually, it turns out that the following code works. Binding inout parameters even works, and the raised exception makes it into Diesel’s DatabaseError. I’m less familiar with PostgreSQL transactions but I may in the future need to write procedures that have a `COMMIT` or `ROLLBACK` statement.

Regarding using a `sql_function!`, is it possible to call it without any associated table, i.e. like

`select my_function();`

Thanks again!

Here is the code.

```auto
    pub fn create(conn: &PgConnection, params: Graph, uuid: Uuid) -> Result<Uuid, ServiceError> {
        let mut out_uuid = Uuid::new_v4();
        let res = diesel::sql_query("call create_proc($1, $2, $3)")
            .bind::<sql_types::Text, _>(params.name)
            .bind::<sql_types::Text, _>(params.desc)
            .bind::<sql_types::Uuid, _>(uuid)
            .bind::<sql_types::Uuid, _>(&out_uuid)
            .execute(conn);
        match res {
            Ok(_) => Ok(true),
            Err(e) => Err(e.into())
        }
    }

```

where:

```auto
create or replace procedure create_proc(
        name in varchar,
        description in varchar,
        uuid in uuid,
        out_uuid inout uuid)
as
$$
declare
        my_id integer;
begin
        select id into my_id from parent_table where uuid = uuid;
        if not found then
                raise exception 'No Source Record for %', uuid;
        end if;

        insert into target_table (name, description, my_id)
        values (name, description, id)
        returning uuid into out_uuid;
end;
$$ language plpgsql;

```

---

<div class="post-metadata">

### Author: ![sean](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.diesel.rs/sean/32/2_2.png) [@sean](https://discourse.diesel.rs/u/sean)
#### Post date: [July 2, 2019, 5:16pm UTC](https://discourse.diesel.rs/t/support-for-postgresql-11-1-stored-procedures/99/4 "2019-07-02T17:16:35Z")

</div>

Yes, see [http://docs.diesel.rs/diesel/fn.select.html](http://docs.diesel.rs/diesel/fn.select.html)

---

<div class="post-metadata">

### Author: ![hrbigelow](https://avatars.discourse-cdn.com/v4/letter/h/eb9ed0/32.png) [@hrbigelow](https://discourse.diesel.rs/u/hrbigelow)
#### Post date: [July 2, 2019, 5:34pm UTC](https://discourse.diesel.rs/t/support-for-postgresql-11-1-stored-procedures/99/5 "2019-07-02T17:34:07Z")

</div>

That’s great, thank you again. I do have a related follow up question - I’ll post it in a separate question.  
Maybe it’s possible with Diesel as well.

Cheers
