# runPrepUpdate getKey returning wrong value

**URL:** <https://forum.inductiveautomation.com/t/runprepupdate-getkey-returning-wrong-value/73478>\
**Category:** Ignition\
**Created:** [May 9, 2023, 2:33pm UTC](https://forum.inductiveautomation.com/t/runprepupdate-getkey-returning-wrong-value/73478 "2023-05-09T14:33:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chad\_Putnam](https://avatars.discourse-cdn.com/v4/letter/c/9fc29f/32.png) [@Chad\_Putnam](https://forum.inductiveautomation.com/u/Chad_Putnam)\
**Post date:** [May 9, 2023, 2:33pm UTC](https://forum.inductiveautomation.com/t/runprepupdate-getkey-returning-wrong-value/73478/1 "2023-05-09T14:33:15Z")

</div>

Hello All,

I am running a very simple insert query that should return the identity key from a MSSql server. When I run the query below, and print the results I get a incrementing values (1316134921, 1316134922, 1316134923 for example). However, that is not the value I expected. It should have been "10000000000010, 10000000000011, 10000000000012...etc"

When I run the query directly on the SQL server, I get the correct value.

```auto
getRFID = '''INSERT INTO RFID_Sequence (prefix, job)
		   VALUES (?,?)
		   SELECT SCOPE_IDENTITY() '''
RFSeq = system.db.runPrepUpdate(getRFID, [RFIDPrefix,jobNumber], 'DBSource', getKey = True)
print RFSeq

```

Any idea what I am doing wrong?

---

<div class="post-metadata">

**Author:** ![KathyApplebaum](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kathyapplebaum/32/7261_2.png) [@KathyApplebaum](https://forum.inductiveautomation.com/u/KathyApplebaum)\
**Post date:** [May 9, 2023, 2:44pm UTC](https://forum.inductiveautomation.com/t/runprepupdate-getkey-returning-wrong-value/73478/2 "2023-05-09T14:44:30Z")

</div>

The docs say that `system.db.runPrepUpdate` returns an integer, and 10000000000010 is too big for an integer. I suspect that 1316134921 is the value you get taking integer overflow into account, but I don't have time right now to verify.

---

<div class="post-metadata">

**Author:** ![Tim\_Carlton](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/tim_carlton/32/52835_2.png) [@Tim\_Carlton](https://forum.inductiveautomation.com/u/Tim_Carlton)\
**Post date:** [May 9, 2023, 2:52pm UTC](https://forum.inductiveautomation.com/t/runprepupdate-getkey-returning-wrong-value/73478/3 "2023-05-09T14:52:01Z")

</div>

Are there any triggers on the table? Is replication enabled on the database server? Either of those might return strange data.

What does the data look like in the database itself ("select top 10 \* from RFID\_Sequence order by [pkid] desc" from SSMS)? Does it have the expected values or the returned values?

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [May 9, 2023, 3:12pm UTC](https://forum.inductiveautomation.com/t/runprepupdate-getkey-returning-wrong-value/73478/4 "2023-05-09T15:12:59Z")

</div>

I'd go with overflow:

```python
longVal = 10000000000010
for i in range(10):
	print longVal+i, (longVal+i) & (2**32-1)

```

Output:

```plaintext
10000000000010 1316134922
10000000000011 1316134923
10000000000012 1316134924
10000000000013 1316134925
10000000000014 1316134926
10000000000015 1316134927
10000000000016 1316134928
10000000000017 1316134929
10000000000018 1316134930
10000000000019 1316134931

```

---

<div class="post-metadata">

**Author:** ![Chad\_Putnam](https://avatars.discourse-cdn.com/v4/letter/c/9fc29f/32.png) [@Chad\_Putnam](https://forum.inductiveautomation.com/u/Chad_Putnam)\
**Post date:** [May 9, 2023, 4:15pm UTC](https://forum.inductiveautomation.com/t/runprepupdate-getkey-returning-wrong-value/73478/5 "2023-05-09T16:15:16Z")

</div>

> [@JordanCClark](#):
>
> `1316134922`

That is what it was...Once I changed my DB data type from bigint to int, I started getting the expected value. Thanks for the help.

---

<div class="post-metadata">

**Author:** ![WillMT10](https://avatars.discourse-cdn.com/v4/letter/w/7feea3/32.png) [@WillMT10](https://forum.inductiveautomation.com/u/WillMT10)\
**Post date:** [May 9, 2023, 4:36pm UTC](https://forum.inductiveautomation.com/t/runprepupdate-getkey-returning-wrong-value/73478/6 "2023-05-09T16:36:46Z")

</div>

You may already be thinking of this, but a word of caution with switching from bigint to int. If that column is an auto incrementing value, you are already over half way to the max value that a signed int column can hold. No idea how much time that gives you with your process, but I just wanted to make sure you're not missing that potential future pain point.

---

<div class="post-metadata">

**Author:** ![Chad\_Putnam](https://avatars.discourse-cdn.com/v4/letter/c/9fc29f/32.png) [@Chad\_Putnam](https://forum.inductiveautomation.com/u/Chad_Putnam)\
**Post date:** [May 9, 2023, 5:11pm UTC](https://forum.inductiveautomation.com/t/runprepupdate-getkey-returning-wrong-value/73478/7 "2023-05-09T17:11:22Z")

</div>

Thanks for the heads up. It should only increment by about 50k per year, so the 1.2 billion or so will be enough. 😃

---

<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:** [February 12, 2024, 7:51pm UTC](https://forum.inductiveautomation.com/t/runprepupdate-getkey-returning-wrong-value/73478/8 "2024-02-12T19:51:07Z")

</div>

An alternative is now available for those out there needing `bigint` primary keys:

> [@Automation Professionals' Integration Toolkit Module](https://forum.inductiveautomation.com/t/automation-professionals-integration-toolkit-module/74934/89):
>
> Automation Professionals is pleased to announce a new release of the Integration Toolkit module. This release adds one new scripting function, [system.db.runPrepInsert()](https://www.automation-pros.com/toolkit/doc/utility.html#runprepinsert). It is very similar to IA's [system.db.runPrepUpdate()](https://docs.inductiveautomation.com/display/DOC81/system.db.runPrepUpdate), intepreting the query, args, database, and tx arguments in the same fashion. The getKey argument is omitted as moot, as this function always retrieves generated keys. The skipAudit optional argument is unimplemented. For Ignition v8.1+: [v2.0.15.240431913](https://www.automation-pros.com/ignition/Integration%20Toolkit-2.0.15.240431913-v81.modl) Do note the docu…
