# Running a stored procedure on a button with python

**URL:** <https://forum.inductiveautomation.com/t/running-a-stored-procedure-on-a-button-with-python/88541>\
**Category:** Ignition\
**Tags:** ignition81, perspective\
**Created:** [April 8, 2024, 8:05pm UTC](https://forum.inductiveautomation.com/t/running-a-stored-procedure-on-a-button-with-python/88541 "2024-04-08T20:05:29Z")\
**Posts on this page:** 6\
**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:** [April 8, 2024, 8:05pm UTC](https://forum.inductiveautomation.com/t/running-a-stored-procedure-on-a-button-with-python/88541/1 "2024-04-08T20:05:30Z")

</div>

Good afternoon, I have a button that I need to run a stored procedure on. Currently my script looks like this:

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

```

Is this the correct way of doing it?  
I am very new to stored procedures.  
I need to pass 2 variables in the stored procedure, how do I go about doing that?

---

<div class="post-metadata">

**Author:** ![josborn](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@josborn](https://forum.inductiveautomation.com/u/josborn)\
**Post date:** [April 8, 2024, 8:38pm UTC](https://forum.inductiveautomation.com/t/running-a-stored-procedure-on-a-button-with-python/88541/2 "2024-04-08T20:38:36Z")

</div>

You'll need to register your parameters before you execute the stored procedure.

```auto
call.registerInParam(index OR name, typeCode, value)
call.registerOutParam(index OR name, typeCode)
call.registerReturnParam(typeCode)

```

---

<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:** [April 8, 2024, 8:47pm UTC](https://forum.inductiveautomation.com/t/running-a-stored-procedure-on-a-button-with-python/88541/3 "2024-04-08T20:47:57Z")

</div>

These are the 2 from the SP on the DB:

@BUGGY\_ID nvarchar(8),  
@TSFO\_ID nvarchar(256)

I can get those from the .props stuff on my project:

```auto
Buggy_ID = self.getSibling("Table_0").props.selection.data[0].Buggy_ID
TSFO_ID = self.getSibling("Table").props.selection.data[0].ID

```

So would it be something like this:

```auto
call = system.db.createSProcCall("dbo.ReserveBuggyInCorral", [IGV_Prd])
	call.registerInParam(@BUGGY_ID, system.db.NVARCHAR, Buggy_ID)
	call.registerInParam(@TSFO_ID, system.db.NVARCHAR, TSFO_ID)
	system.db.execSProcCall(call)

```

---

<div class="post-metadata">

**Author:** ![josborn](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@josborn](https://forum.inductiveautomation.com/u/josborn)\
**Post date:** [April 8, 2024, 8:50pm UTC](https://forum.inductiveautomation.com/t/running-a-stored-procedure-on-a-button-with-python/88541/4 "2024-04-08T20:50:50Z")

</div>

That looks right, but is `[IGV_Prd]` what the database connection in ignition is actually named and if it is i think it needs quotes around it?

Oh, i think you need to change this too:

```auto
call.registerInParam("BUGGY_ID", system.db.NVARCHAR, Buggy_ID)
call.registerInParam("TSFO_ID", system.db.NVARCHAR, TSFO_ID)

```

---

<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:** [April 8, 2024, 9:03pm UTC](https://forum.inductiveautomation.com/t/running-a-stored-procedure-on-a-button-with-python/88541/5 "2024-04-08T21:03:40Z")

</div>

Yeah it should look like this:

```auto
 call = system.db.createSProcCall("dbo.ReserveBuggyInCorral", 'IGV_Prd')
 call.registerReturnParam(system.db.INTEGER)
 call.registerInParam("BUGGY_ID", system.db.NVARCHAR, Buggy_ID)
 call.registerInParam("TSFO_ID", system.db.NVARCHAR, TSFO_ID)
 system.db.execSProcCall(call)

```

Thanks for that

That looks like its running, I get no errors. I return a "0" which isnt a good or a bad thing. Ill need to put some test data into that DB to make sure. I should return a 1 if its all good and 0 if not.

In the SQL server, when it runs it does have an error handler on it. In this case returns:

```auto
Wrong Order ID or unable to retrieve order informations
Msg 50000, Level 1, State 1

```

Can I get this to return that also?

---

<div class="post-metadata">

**Author:** ![josborn](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@josborn](https://forum.inductiveautomation.com/u/josborn)\
**Post date:** [April 8, 2024, 9:35pm UTC](https://forum.inductiveautomation.com/t/running-a-stored-procedure-on-a-button-with-python/88541/6 "2024-04-08T21:35:15Z")

</div>

I am not sure about that, I've use a returnParamater to get a record id before, but that's about the extent of my experience.
