HOWTO: Retrieve Identity Column After Insert Using RDOLast reviewed: November 26, 1997Article ID: Q170147 |
The information in this article applies to:
SUMMARYSQL Server Identity columns automatically increment their value when a new record is added to the table, which makes them very useful for a unique primary key. When using RDO, you may want to know what the new value of the Identity column is after inserting a new record with the RDO Addnew/Update methods, or with a SQL INSERT statement. SQL Server maintains an environment variable named @@IDENTITY that stores the value of the last Identity column added. This environment variable can be queried several different ways from RDO.
MORE INFORMATIONThe following two code examples demonstrate how the value of the Identity column is retrieved from the SQL Server Pubs database using RDO, after a new record is inserted. For testing purpose, a temporary table, IColTest, is created in Pubs database with one Identity column and one VarChar column. To create a temporary table and index in SQL Server, select Pubs database, then place the following Create Table T-SQL in the SQL window of ISQL/W and execute it.
Create Table IColTest (Id_Col int Identity, F2 VarChar(30) Null) Create Unique Index IIndex on IColTest(Id_Col) Example OneThis example uses a stored procedure to insert a row by taking parameters passed from RDO, and then returns the value of @@IDENTITY in a OUTPUT parameter. Task One: Create the Stored Procedure To create this stored procedure, place the following Create Procedure T-SQL in the SQL window of ISQL/W and execute it:
Create Procedure MyInsert @FieldVal Varchar(30), @id Int OUTPUT AS Insert Into IColTest (F2) Values(@FieldVal) Select @id = @@IdentityTask Two: Create the VB Code
Example TwoYou can also do a Select @@IDENTITY with an OpenResultset after inserting a row without creating a stored procedure on the SQL server.
REFERENCESFor additional information of User Connection Designer, please see the following article in the Microsoft Knowledge Base:
ARTICLE-ID: Q166281 TITLE: HOWTO: Create and Implement a UserConnection (c) Microsoft Corporation 1997, All Rights Reserved.Contributions by Adrian Chiang, Microsoft Corporation Keywords : vb5all kbhowto Version : 5.0 Platform : WINDOWS |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |