HOWTO: Retrieve Identity Column After Insert Using ODBCDirectLast reviewed: December 8, 1997Article ID: Q177736 |
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. SQL Server maintains an environment variable named @@IDENTITY that stores the value of the last Identity column added and can be queried several different ways. This article demonstrates how the value of the Identity column is retrieved from the SQL Server Pubs database using ODBCDirect.
MORE INFORMATIONThe following code example demonstrates two different ways to retrieve the Identity column. One calls a stored procedure, MyInsert, to insert a row, and returns the value of @@IDENTITY in an OUTPUT parameter; the other, issues a Select @@IDENTITY with an OpenRecordset after inserting a row without creating a stored procedure on the SQL server.
Step 1: Create Test Table in SQL ServerFor testing purpose, table IColTest, is created in Pubs database with one Identity column and one VarChar column. To create a 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) Step 2: Create Visual Basic Code
REFERENCESFor additional information, please see the following article in the Microsoft Knowledge Base:
ARTICLE-ID: Q170147 TITLE: HOWTO: Retrieve Identity Column After Insert Using RDO(c) Microsoft Corporation 1997, All Rights Reserved. Contributions by Adrian Chiang, Microsoft Corporation Keywords : vb5all vb5howto Technology : kbodbc Version : WINDOWS:5.0 Platform : WINDOWS Issue type : kbhowto |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |