OUTPUT INSERTED.ID

636 views
Skip to first unread message

Hugo Magalhaes

unread,
Jul 26, 2013, 2:31:41 PM7/26/13
to node-t...@googlegroups.com
Hi,

I'm trying to add a new record to a table and I need to get the Identity from the added row.
My query is: INSERT INTO MyTable (Name, Description) OUTPUT INSERTED.ID VALUES ('Name', 'Description')
This works in SQL Server Management Studio but I don't know how to get the ID in Tedious.

Can you please help me?

Thanks.
Best regards,
Hugo

Ciaran

unread,
Jul 26, 2013, 6:41:12 PM7/26/13
to node-t...@googlegroups.com
Can you not use select scope_identity() (make sure you're using the same connection to the db that did the insert) ?
- cj
--
You received this message because you are subscribed to the Google Groups "tedious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-tedious...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Hugo Magalhaes

unread,
Jul 29, 2013, 7:23:24 AM7/29/13
to node-t...@googlegroups.com
Hi,

Thanks for your help but your suggestion it didn't worked for me. 
I've tried also:
- adding the scope_identity after the insert statement: "INSERT INTO *************; select scope_identity();
- making the "select scope_identity();" request in the same transaction as the insert sql.

And all these methods did'nt work so I've changed to a stored procedure that does the insert and then send the identity back.
That was the only way I could made it work.

Best regards,
Hugo


On Friday, July 26, 2013 11:41:12 PM UTC+1, JavaJunky wrote:
Can you not use select scope_identity() (make sure you're using the same connection to the db that did the insert) ?
- cj

On Friday, July 26, 2013, Hugo Magalhaes wrote:
Hi,

I'm trying to add a new record to a table and I need to get the Identity from the added row.
My query is: INSERT INTO MyTable (Name, Description) OUTPUT INSERTED.ID VALUES ('Name', 'Description')
This works in SQL Server Management Studio but I don't know how to get the ID in Tedious.

Can you please help me?

Thanks.
Best regards,
Hugo

--
You received this message because you are subscribed to the Google Groups "tedious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-tedious+unsubscribe@googlegroups.com.

Mike Pilsbury

unread,
Jul 29, 2013, 7:36:41 AM7/29/13
to node-t...@googlegroups.com
I think that sending it back (as the result of "SELECT @ident = @@IDENTITY" or in a stored procedure's output parameter) is probably the only way.

http://support.microsoft.com/kb/313130 suggests that sort of approach when using JDBC. As many JDBC drivers for SQL Server are built on top of the TDS protocol, just as tedious is, this I suspect that it is the only way.


To unsubscribe from this group and stop receiving emails from it, send an email to node-tedious...@googlegroups.com.

Ciaran

unread,
Jul 29, 2013, 8:23:58 AM7/29/13
to node-t...@googlegroups.com
Hmm, there should be no technical difference between @@IDENTITY or scope_identity() .. but it is likely scope_identity() is the one you *really* want, or you can end up being very confused by trigger-generated records :/
Message has been deleted

ev...@curapps.com

unread,
Nov 15, 2013, 10:50:31 PM11/15/13
to node-t...@googlegroups.com
This works for me with SQL Server 2005 and 2008.  

```javascript
    INSERT INTO table (a, b, c) OUTPUT Inserted.id VALUES (a, b, c) 
```

Then add a callback handler for the `row` event for the request, something like:

```javascript
    req.on('row', function(cols) {
      var o = cols[0]
      var id = o.value
      if (id) output.insertId = id
    })
```
Reply all
Reply to author
Forward
0 new messages