Retrieving the identity of a newly inserted row in a database (Access and SQL server)

Hi,

I've wondered about thsi for a while and would like ot get a stragiht answer. Googling and looking through past threads in this forum aren't helping me.

In pipeline Pilot, using the SQL components (Insert, Select or General - I'm not fussy), i'd like to append to my current data record the Identity of the row into which it is inserted in a database.

At the moment I'm using an INSERT component:

INSERT into (Column1,Column2,...)

VALUES (?, ?, ...)

followed by a SELECT component:

Select @@IF


This seems to work but forums on the web discourage the usage of  the @@IC (and just now I've seen varying numbers of records - albeit identical - being returned using this statement)

It also seems clunky and I wonder if the statements can't be combined into a single component. I've tried this but haven't been able to work out the pairing of the syntax with the SQL Parameter Mapping and SQL Parameter Types.

Yet another thing I'm not sure about is if the syntax / functionality is different when quering a DB hosted on a SQL Server or MSAccess.

A Dummy's guide on how to do this would be most appreciated.

Oh while I'm at it - if I have to resort to usinghte @@IN "Expr100/3000" and I have to rename using

rename(ithpropertyname(numproperties()),'MyFavPropName').

I'm still on PP v8.5 btw


As always many thanks for the advice and cooment,

Tris

Answers 6
JH
JH 2013-11-07

Hi Tristan,

It's a little unclear whether you are trying to do this with Oracle or SQL Server or MSAccess.  I think the answer to you question is probably yes, it is different depending which type of DB it is.

However, if it's Oracle have you had a look at this thread?

https://community.accelrys.com/message/18042#18042

I believe it contains the answer you need complete with an example protocol from Malcolm.

If it is SQLServer then I think according to this thread

http://stackoverflow.com/questions/17993606/sql-server-return-the-value-of-identity-column-after-insert-statement

you may be able to use:

Insert into TBL (Name, UserName, Password) Output Inserted.IdentityColumnName

Values ('example', 'example', 'example')

which is probably a very similar implementation to the "RETURNING INTO" statement used in the Oracle version.  However, I only have significant experience with Oracle so somebody more familiar with SQLServer may be able to give you a better answer on that one.

Best,

Jennie

TM
TM 2013-11-07

Hi Jennifer,

Thanks for the response - to clarify I'm most interested in using this for SQL server and Access (the clue is in the title - edit - on second reading I guess it could be ambiguous, my apologies).

Yep I'd seen the Oracle-based thread and hadn't any success with that method which was why I was wondering if it was specifically different for SQL Server and Access.

i stumbled across similar threads to The StackOverflow one and I've tried that syntax too - however as that forum isn't PP-based there's no information there on how to get any value that is succesfully queried into the Pipeline Pilot data record - I'm guessing you'd need the correct combination of SQL Parameter mappings and SQL Parameter Types.

Best,

Tristan

IK
IK 2013-11-07

Hi Tristan,

Here is an example protocol showing how to do this on SQL Server. I'm not sure if this will work on Access or not, but it's worth a shot.

Ian Kerman

Platform Support

comment imagecomment image
JH
JH 2013-11-07

Hi Tristan,

No, you are right, it's clear from the title -- clearly I have not yet adjusted to Daylight Savings Time and it was time for my 4 PM coffee even though it was only 3 PM.

Sadly I don't have access to a SQLServer DB to see if I could get it working, but if I did then based on my experience using the components with Oracle I would try:

insert into TBL(col1, col2, col3) output inserted.identity into ?

values(?,?,?)

parameter mapping = identity,val1,val2,val3  /* note that identity comes first because it is the first in the statement */

parameter types = out,in,in,in

I am always of the "let's try it and see" persuasion though, so you may wish to wait for an actual person who knows to see this thread and send an answer. :-)

Best,

Jennie

AV
AV 2013-11-07

Hi Ian/Tristan/Jennifer,

Coincidentally I had the same question last week and I had sought help from both Ian Kerman & Keith Burdick.

Based on what Ian/Keith mentioned and digging around elsewhere, I found this link - http://stackoverflow.com/questions/1864952/getting-the-autoincremnt-value-after-inserting-record-using-sql-server  - it is suggested to use Scope_Identity() instead of @@Idenity. Here is the relevant text from that link:

  • ident_current ('TableName') for a specific table, not limited by scope and session
  • @@IDENTITY last ID in current session
  • scope_identity() last ID in current session, current scope
  • You should use scope_identity() instead of @@identity. If there are any triggers fired on the insert, @@identity won't hold the value you're expecting

Based on all of the above, I ended up using 'SQL Select for Each data' with these statements (this is for work on SQL Server)

INSERT into (, , etc...)

VALUES (?, ?)

SELECT SCOPE_IDENTITY() AS 'New_ID_Inserted'

Based on the last line there, 'New_ID_Inserted' will be the identity of the newly inserted row

HTH

Anand

TM
TM 2013-11-07

Thanks Ian and Anand,

I already had pretty much what is in the example protocol Ian had attached.

I've since gone through some protocols from a previous job - I've actually got code that matches what Anand has - that works well in SQL server definetly.

Now I'm working in the Aceess world it looks like the Scope_Identity() fiunction doesn't work there - unrecognised command so I guess we're stuck with using @@Identity if working in that environment.

It also seems that it's not possible to roll two SQL statements into a single component - again only for Access - perfectly doable in SQL Server.

Naming the Property using the 'AS' command also seems to work OK - I know I've tried this before and can't work out why I thought this wasn't working for me - I'm with Jen and will blame it on the DST time shift.

I'm gonig to mark this as answered, thanks all for your contributions to this thread