# How to get last inserted row into a database table from a named query

**URL:** <https://forum.inductiveautomation.com/t/how-to-get-last-inserted-row-into-a-database-table-from-a-named-query/108553>\
**Category:** Ignition\
**Created:** [July 16, 2025, 1:29pm UTC](https://forum.inductiveautomation.com/t/how-to-get-last-inserted-row-into-a-database-table-from-a-named-query/108553 "2025-07-16T13:29:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [July 16, 2025, 1:29pm UTC](https://forum.inductiveautomation.com/t/how-to-get-last-inserted-row-into-a-database-table-from-a-named-query/108553/1 "2025-07-16T13:29:38Z")

</div>

I recall this being possible but I haven't been able to make it work.

I have a named query

```python
INSERT INTO dbo.RecipeLog(RecipeDataID, UpdatedBy)
VALUES (:RecipeDataID, :UpdatedBy)

SELECT SCOPE_IDENTITY() AS LastInsertedID:
```

I would expect this to return 143 or whatever the last row inserted is (there is an auto incrementing primary key on the table) but it instead returns 1 (the first row).

I think last time I put the SCOPE\_IDENTITY statement inside a stored procedure is why it worked. Is there a way to make this work inside a named query?

---

<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:** [July 16, 2025, 1:50pm UTC](https://forum.inductiveautomation.com/t/how-to-get-last-inserted-row-into-a-database-table-from-a-named-query/108553/2 "2025-07-16T13:50:43Z")

</div>

Use the `getKey` argument when running the NQ. You should not be using SCOPE\_IDENTITY() yourself.
