Basic knowledge
1. the identity column cannot be directly updated by the user. It is automatically maintained by the system.
2. The data type of this column must be numeric: int, smallint, tinyint, decimal or numeric with scale 0.
3. This column cannot be null.
4. You cannot set the default value in this column.
5. The increment value can only be an integer (for example, 1, 2,-3 ). It cannot be a decimal or 0.
6. The base value (SEED) can be set by the user. The default value is 1.
Understanding @ identity
@ Identity returns the value of the last inserted identity. These operations include insert, select into, or bulk copy. If a record is inserted to other tables without the identity column, the system sets it to null. If multiple rows of records are inserted into the identity table, @ identity indicates the last generated value. If a trigger is triggered and the trigger inserts data to another table with an identity column, @ identity returns the value generated by the trigger. If the table inserted by this trigger does not contain the identity column, @ identity is null. If the insert operation fails, the @ identity value will still increase, so identity does not guarantee data continuity.
@ Identity is the global variable of the current connection and is only valid for the current connection. That is to say, if the connection is closed and then reconnected, @ identity is null. In ADO, @ identity makes sense when the connection object is opened or closed, that is, it is valid within the scope of the connection object. In the MTS component, from opening a connection to an explicit close connection (connection. Close) or before calling setabort or setcomplete, @ identity makes sense during this period.
The truncate TABLE statement causes the identity column to start computing again.
Obtain the value of @ identity.
There are three methods (the following code uses VBScript)
Method 1:
Dim Conn, strsql, RS
Set conn = Createobject ("ADODB. Connection ")
'Open a connection to the database
Conn. Open ("DSN = mydsn; uid = myuid; Pwd = mypwd ;")
'Insert a new record into the table
Strsql = "insert into mttable (columnname) values ('something ')"
'Execute the SQL statement
Conn. Execute (strsql)
'Get the @ identity.
Strsql = "select @ identity as newid"
Set rs = conn. Execute (lssql)
Newid = Rs. Fields ("newid"). Value
'Close the connection
Conn. Close ()
Set conn = nothing
Method 2 (limited to ADO 2.0 or above ):
Dim Conn, strsql, RS
Set conn = Createobject ("ADODB. Connection ")
'Open a connection to the database
Conn. Open ("DSN = mydsn; uid = myuid; Pwd = mypwd ;")
'Insert a new record into the table
Lssql = "insert into mytable (columnname) values ('something ');"&_
"Select @ identity as newid ;"
'Execute the SQL statement
Set rs = conn. Execute (lssql)
'Get the second resultset Into A recordset object
Set rs = Rs. nextrecordset ()
'Get the inserted ID
Newid = Rs. Fields ("newid"). Value
'Close the connection
Conn. Close ()
Set conn = nothing
Method 3:
Dim Conn, strsql, RS
Set conn = Createobject ("ADODB. Connection ")
'Open a connection to the database
Conn. Open ("DSN = mydsn; uid = myuid; Pwd = mypwd ;")
'Insert a new record into the table
Strsql = "set nocount on ;"&_
"Insert into mytable (columnname) values ('something ');"&_
"Select @ identity as newid ;"
'Execute the SQL statement
Set rs = conn. Execute (lssql)
'Get the inserted ID
Newid = Rs. Fields ("newid"). Value
'Close the connection
Conn. Close ()
Set conn = nothing
# Database Technology