# Microsoft SQL - Get id inserted

**URL:** <https://community.n8n.io/t/microsoft-sql-get-id-inserted/13920>\
**Category:** Questions\
**Tags:** node, microsoft-sql\
**Created:** [May 12, 2022, 1:40pm UTC](https://community.n8n.io/t/microsoft-sql-get-id-inserted/13920 "2022-05-12T13:40:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![michael75](https://avatars.discourse-cdn.com/v4/letter/m/8e8cbc/32.png) [@michael75](https://community.n8n.io/u/michael75)\
**Post date:** [May 12, 2022, 1:40pm UTC](https://community.n8n.io/t/microsoft-sql-get-id-inserted/13920/1 "2022-05-12T13:40:52Z")

</div>

I’m using Microsoft SQL node in order to insert data from my Wokflow.

First of all, i try to user Execture Query but this option doesn’t loop. Could you confirm this bug?

Then i use Insert instead of Execture Query. In order to bind my DB data to my Workflow data i user set node. Like that i can set my\_value\_in\_db = MyResultFromNode. Then in my Microsoft SQL node i only put my\_value\_in\_db and it’s working. Is there a way to directly set my\_value\_in\_db = MyResultFromNode in columns of Microsoft SQL node?

At the end, i try to get back my id\_just\_created so in my Microsoft SQL node columns i set my\_value\_in\_db, id\_just\_created. Of course, SQL server say that i’m not allow to insert id\_just\_created. How can i have my id\_just\_created back?

Many thanks

---

<div class="post-metadata">

**Author:** ![MutedJam](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/mutedjam/32/70851_2.png) [@MutedJam](https://community.n8n.io/u/MutedJam)\
**Post date:** [May 13, 2022, 7:45am UTC](https://community.n8n.io/t/microsoft-sql-get-id-inserted/13920/2 "2022-05-13T07:45:40Z")

</div>

Hi @michael75,

> First of all, i try to user Execture Query but this option doesn’t loop. Could you confirm this bug?

This is the intended behaviour for most database nodes ([here’s the list](https://docs.n8n.io/flow-logic/looping/#node-exceptions)), including the Microsoft SQL Server one. You could split up your execution in batches of 1 and then process each batch individually like so:

> Is there a way to directly set my\_value\_in\_db = MyResultFromNode in columns of Microsoft SQL node?

Yes, when executing queries, the node supports [expressions](https://docs.n8n.io/code-examples/expressions/). Simply select the node you want to read data from in the expression editor and the required expression is added:

 ![image](https://europe1.discourse-cdn.com/n8n/original/2X/8/8accf671746fa35bf1d802372778d56aba08c2c3.png)

> At the end, i try to get back my id\_just\_created so in my Microsoft SQL node columns i set my\_value\_in\_db, id\_just\_created. Of course, SQL server say that i’m not allow to insert id\_just\_created. How can i have my id\_just\_created back?

I don’t think there is any built-in logic for this. But you could simply select the top(1) rows from the table you have just inserted data into by running two queries like so for each execution of your node:

```sql
INSERT INTO users (first_name, last_name) VALUES ('{{$json["first_name"]}}', '{{$json["last_name"]}}');
SELECT TOP(1) * FROM users ORDER BY id DESC;

```

I’ve also included this into my example from your first question.

---

<div class="post-metadata">

**Author:** ![michael75](https://avatars.discourse-cdn.com/v4/letter/m/8e8cbc/32.png) [@michael75](https://community.n8n.io/u/michael75)\
**Post date:** [June 7, 2022, 2:27pm UTC](https://community.n8n.io/t/microsoft-sql-get-id-inserted/13920/3 "2022-06-07T14:27:14Z")

</div>

Many thanks for your help
