By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,226 Members | 1,032 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,226 IT Pros & Developers. It's quick & easy.

Retrieve last inserted id

P: n/a
Hi All,

Using classic ASP applciation with DB2 (just adding it to the apps
quiver of DBs). I am trying to do an insert and then retrieve the
auto-increment field's value using the standard ADO call of

rsForm.Update
nFormStructRecId = rsForm("record_id")
rsForm.Close

The problem is that the value comes back as 0. How should I fix the
problem?

Regards

phpster

Nov 12 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
lostboy wrote:
Hi All,

Using classic ASP applciation with DB2 (just adding it to the apps
quiver of DBs). I am trying to do an insert and then retrieve the
auto-increment field's value using the standard ADO call of

rsForm.Update
nFormStructRecId = rsForm("record_id")
rsForm.Close

The problem is that the value comes back as 0. How should I fix the
problem?

Regards

phpster

Which version of DB2 are you on.
Also what's the table definition (do you use a SEQUENCE or an IDENTITY
columns)?
How does the INSERT statement look like.

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #2

P: n/a
v8.1 windows

ado insert
sequences

CODE
sSQL = "select * from form_structure where form_id = '" & nHeaderID
& "' and field_name = '" & sFldName & "' "

'use ADO to get the record id number back

rsForm.open sSQL, PortalDB, adOpenDynamic, adLockOptimistic

if rsForm.EOF and rsForm.BOF then
rsForm.AddNew
' sThisSortOrder = 999 'move the newly added record to last
place
end if

rsForm("field_name") = lcase(sFldName)
rsForm("sort_order") = nSortCounter 'current sort order
rsForm("form_id") = nHeaderID
rsForm("data_type") = lcase(sDefaultDataType)

rsForm.Update

nFormStructRecId = rsForm("record_id")

Bastien

Nov 12 '05 #3

P: n/a
lostboy wrote:
v8.1 windows

ado insert
sequences

CODE
sSQL = "select * from form_structure where form_id = '" & nHeaderID
& "' and field_name = '" & sFldName & "' "

'use ADO to get the record id number back

rsForm.open sSQL, PortalDB, adOpenDynamic, adLockOptimistic

if rsForm.EOF and rsForm.BOF then
rsForm.AddNew
' sThisSortOrder = 999 'move the newly added record to last
place
end if

rsForm("field_name") = lcase(sFldName)
rsForm("sort_order") = nSortCounter 'current sort order
rsForm("form_id") = nHeaderID
rsForm("data_type") = lcase(sDefaultDataType)

rsForm.Update

nFormStructRecId = rsForm("record_id")

Bastien

Bear with me I'm ADO ignorant.
If you are using sequence how do you generate teh new value?
Do you have an INSERT trigger on FORM_STRUCTURE?
You can get the value back through two means:
Select back "SELECT PREVIOUS VALUE FOR <seqname> FROM SYSIBM.SYSDUMMY1"
or Do the insert expliictly:

SELECT <idcol> FROM NEW TABLE(INSERT INTO FORM_STRUCTURE .....)

Cheers
Serge

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.