Solved

Inserted Records

Posted on 1998-07-06
3
435 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Ransomware continues to grow in reach and sophistication, putting data everywhere at risk. Learn how to avoid being caught in its sinister clutches with these 11 key tips.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

820 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