# Clarification executing a stored procedure

**URL:** <https://forum.inductiveautomation.com/t/clarification-executing-a-stored-procedure/82807>\
**Category:** Ignition\
**Tags:** ignition81, perspective\
**Created:** [December 6, 2023, 7:45pm UTC](https://forum.inductiveautomation.com/t/clarification-executing-a-stored-procedure/82807 "2023-12-06T19:45:11Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![mjohnson0238](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@mjohnson0238](https://forum.inductiveautomation.com/u/mjohnson0238)\
**Post date:** [December 6, 2023, 7:45pm UTC](https://forum.inductiveautomation.com/t/clarification-executing-a-stored-procedure/82807/1 "2023-12-06T19:45:11Z")

</div>

So, first time trying to execute an SP. Wondering if I think this is right:

I create a project, in the Gateway Event Scripts, set a scheduled script - based on whatever occurrence I want it to execute at, then the script would look like this:

```auto
system.db.execSProcCall(dbo.UpsertPlt65Users)

```

?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [December 6, 2023, 7:50pm UTC](https://forum.inductiveautomation.com/t/clarification-executing-a-stored-procedure/82807/2 "2023-12-06T19:50:52Z")

</div>

No; you first need to create the stored procedure call object, then execute it. Take a look at the examples:  
[https://docs.inductiveautomation.com/display/DOC81/system.db.createSProcCall](https://docs.inductiveautomation.com/display/DOC81/system.db.createSProcCall)

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 6, 2023, 7:52pm UTC](https://forum.inductiveautomation.com/t/clarification-executing-a-stored-procedure/82807/3 "2023-12-06T19:52:11Z")

</div>

Not even close.

The documentation for [system.db.execSProcCall](https://docs.inductiveautomation.com/display/DOC81/system.db.execSProcCall) clearly states that its one argument must come from a call to [system.db.createSProcCall](https://docs.inductiveautomation.com/display/DOC81/system.db.createSProcCall).

That latter page has examples for you to examine.

---

<div class="post-metadata">

**Author:** ![mjohnson0238](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@mjohnson0238](https://forum.inductiveautomation.com/u/mjohnson0238)\
**Post date:** [December 6, 2023, 7:56pm UTC](https://forum.inductiveautomation.com/t/clarification-executing-a-stored-procedure/82807/4 "2023-12-06T19:56:09Z")

</div>

something like this:

```auto
call = system.db.createSProcCall("dbo.UpsertPlt65Users", [IgnitionHud])
system.db.execSProcCall(call)

```

The table created from the SP is already existing in the DB, is that an issue?

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [December 6, 2023, 7:58pm UTC](https://forum.inductiveautomation.com/t/clarification-executing-a-stored-procedure/82807/5 "2023-12-06T19:58:24Z")

</div>

Honestly, just avoid the headache\* and call it directly either from a Named Query, or PrepQuery. Particularly if you have many parameters that will need to be registered.

\*If your flavor of DB's JDBC driver happens to be somewhat "agnostic" to SQL scripts. If not you may have issues not using the `SProcCall` family of functions.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 6, 2023, 8:00pm UTC](https://forum.inductiveautomation.com/t/clarification-executing-a-stored-procedure/82807/6 "2023-12-06T20:00:34Z")

</div>

What is `[IgntionHud]` ? The second argument to `createSProcCall()` must be a data source name, not a list of parameters.

Parameters have to be registered one by one. As shown in the examples. (!!!!)

---

<div class="post-metadata">

**Author:** ![mjohnson0238](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@mjohnson0238](https://forum.inductiveautomation.com/u/mjohnson0238)\
**Post date:** [December 6, 2023, 8:04pm UTC](https://forum.inductiveautomation.com/t/clarification-executing-a-stored-procedure/82807/7 "2023-12-06T20:04:06Z")

</div>

Alright,

**system.db.createSProcCall(procedureName, [database], [tx], [skipAudit])**

my procedureName is dbo.UpsertPlt65Users,  
my [database] is IgnitionHud, that just what our DB name is in SQL

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [December 6, 2023, 8:10pm UTC](https://forum.inductiveautomation.com/t/clarification-executing-a-stored-procedure/82807/8 "2023-12-06T20:10:54Z")

</div>

```auto
call = sysetem.db.createSProcCall('dbo.UpsertPlt65Users','IgnitionHud')
system.db.execSProcCall(call)

```

Assuming that the DataSource is also named 'IgnitionHud'.

In this instance that is the name given to the Database Connection when it was added to the gateway. It can be completely different from the DB name in SQL.

The square brackets in the documentation indicate that the parameter is optional. So technically the default data source would be used if you left the parameter out. I recommend always suppling the data source name.

You can find the actual data source name in the gateway at Config\>Database\>Database Connections

> [@mjohnson0238](#):
>
> The table created from the SP is already existing in the DB, is that an issue?

This depends on the Stored Procedure and what it is doing.

---

<div class="post-metadata">

**Author:** ![mjohnson0238](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@mjohnson0238](https://forum.inductiveautomation.com/u/mjohnson0238)\
**Post date:** [December 6, 2023, 8:15pm UTC](https://forum.inductiveautomation.com/t/clarification-executing-a-stored-procedure/82807/9 "2023-12-06T20:15:25Z")

</div>

OK, so it seems, I needed single quotes instead of double quotes.

The SP, according to contents should add and remove users if they are considered "active"

---

<div class="post-metadata">

**Author:** ![mjohnson0238](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@mjohnson0238](https://forum.inductiveautomation.com/u/mjohnson0238)\
**Post date:** [December 6, 2023, 8:24pm UTC](https://forum.inductiveautomation.com/t/clarification-executing-a-stored-procedure/82807/10 "2023-12-06T20:24:17Z")

</div>

Thanks everyone for their help!

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 6, 2023, 9:38pm UTC](https://forum.inductiveautomation.com/t/clarification-executing-a-stored-procedure/82807/11 "2023-12-06T21:38:53Z")

</div>

> [@mjohnson0238](#):
>
> I needed single quotes instead of double quotes.

No, you needed either kind of quotes around the string database name, instead of square brackets.

---

<div class="post-metadata">

**Author:** ![mjohnson0238](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@mjohnson0238](https://forum.inductiveautomation.com/u/mjohnson0238)\
**Post date:** [December 6, 2023, 10:19pm UTC](https://forum.inductiveautomation.com/t/clarification-executing-a-stored-procedure/82807/12 "2023-12-06T22:19:12Z")

</div>

Yes sir, and that also.
