Solved

Inserted Records

Posted on 1998-07-06
3
421 Views
Last Modified: 2008-03-10
I am inserting a record into a table that has a trigger that generates a new key field value for the inserted record.
I want to use sql to retrieve the ID field of the latest record inserted immedialtely after it has been added to the DB
I am using the following code and am having trouble doing so:
strSQL = "insert into users (site_id, title, lastname, firstname, country, city, phone, fax, userid, op_current_user) values ('" & txtOrg.Text & "', '" & cTitle.Text & "', '" & txtLastName.Text & "', '" & txtFirstname.Text & "', '" & CStr(cCode) & "', '" & txtCity.Text & "', '" & txtPhone.Text & "', '" & txtFax.Text & "', 0, '2')"
Set rsUser = cn.OpenResultset(strSQL)/*This Works OK*/
Set rsUser = cn.OpenResultset("Select * from users,inserted where users.userid = inserted.userid")/*This generates an error*/
txtUID.Text = rsUser!userid
Could anyone provide me with the proper syntax for what i am trying to achieve?
0
Comment
Question by:Coates
  • 2
3 Comments
 

Author Comment

by:Coates
ID: 1098154
I get The Following Error Message using this code:
"Sybase does not allow more than one active statementwhen retrieving results without a cursor".
Is there an alternative method to retrieve the latest inserted key field value
0
 

Author Comment

by:Coates
ID: 1098155
I changed the code format to the following:
strSQL = "insert into users (site_id, title, lastname, firstname, country, city, phone, fax, userid, op_current_user) values ('" & txtOrg.Text & "', '" & cTitle.Text & "', '" & txtLastName.Text & "', '" & txtFirstname.Text & "', '" & CStr(cCode) & "', '" & txtCity.Text & "', '" & txtPhone.Text & "', '" & txtFax.Text & "', 0, '2')"
    strSQL = strSQL & " Select userid from users,inserted"
    Set rsUser = cn.OpenResultset(strSQL)
    txtUID = rsUser!userid
But i got another error:
"Inserted not found.Specify owner.objectname or use sp_help to check whether object exists"
I thought that the inserted table holds information about any changesmade to the table
The sybase version im using is11.0.2.2
0
 
LVL 7

Accepted Solution

by:
spiridonov earned 50 total points
ID: 1098156
INSERTED table is only available in trigger, you can not see it from anywhere else. If your user_id is autogenerated (IDENTITY), you can use @@IDENTITY global variable, that contains last inserted by current connection identity value .
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
We have come a long way with backup and data protection — from backing up to floppies, external drives, CDs, Blu-ray, flash drives, SSD drives, and now to the cloud.
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now