With select Ident_current (tablename)
Ident_current
Returns the identity value that was last generated for the specified table in any session and in any scope.
Grammar
Ident_current (' table_name ')
Parameters
table_name
is the name of the table whose identity value you want to return. The TABLE_NAME data type is varchar and has no default value.
return type
sql_variant
Comments
Ident_current is similar to the Microsoft®sql server™2000 identification function scope_identity and @ @identity. All three functions return the last-generated identity value. However, they differ in scope and session that define "last".
Ident_current returns the identity value that was last generated for any session and for a specific table in any scope.
@ @identity returns the identity value that was last generated for any table in all scopes of the current session.
Scope_identity returns the identity value that was last generated for any table in the current session and in the current scope
in ASP tutorial. Net There are two ways to get the value of the self-added column, the way in which the output parameters are used in the stored procedure, the other is to write the SQL statement in the code, and then get the value by executing the ExecuteScalar () method. The following are examples of these two cases, the assumption that there are two kinds of tables, a class information table class, a Student information table student, the two tables is a one-to-many relationship,
For example, there are two tables T1 and T2, and an INSERT trigger is defined on the T1. When a row is inserted into the T1, the trigger is fired and a row is inserted into the T2. This scenario demonstrates two scopes: inserts on the T1, and inserts through triggers in T2.
Suppose both T1 and T2 have identity columns, @ @identity and scope_identity will return different values at the end of the INSERT statement on T1. @ @identity Returns the value of the last identity column inserted in any scope in the current session. This is the value that is inserted in the T2. Scope_identity () will return the identity value inserted in T1. This is the final insertion that takes place within the same scope. If the scope_identity () function is called before any INSERT statement acts on an identity column in the scope, the function returns a null value
The database tutorial is designed as follows:
CREATE TABLE Class
(
ClassID int Identity (1,1) primary key,
ClassName nvarchar (20)
)
CREATE TABLE Student
(
StudentID int Identity (1,1) primary key,
ClassID int foreign key references class (ClassID),
Studentname nvarchar (10)
)
1. Using stored procedures to obtain self-added columns
Create proc Proinsertclass
(
@classname varchar (20),
@classid int Output
)
As
INSERT into Class (classname) values (@classname)
Set @classid =scope_identity ()
The corresponding ASP.net tutorial code is as follows:
SqlConnection con = new SqlConnection ("server=.; database=test;uid=sa;pwd=123456 ");
SqlCommand cmd = new SqlCommand ("Proinsertclass", con);
Cmd.commandtype = CommandType.StoredProcedure;
sqlparameter[] Pars ={new SqlParameter ("@classname", SqlDbType.NVarChar),
New SqlParameter ("@classid", SqlDbType.Int)
};
Pars[0].value = "Computer class";
Pars[1].direction = ParameterDirection.Output;
Cmd.parameters.addrange (pars);
Con.open ();
Cmd.executenonquery ();
Con.close ();
int classid = Convert.ToInt32 (Pars[1].value);
2. Write the SQL statement in your code and then execute the ExecuteScalar () method to get the value, as follows:
SqlConnection con = new SqlConnection ("server=.; database=test;uid=sa;pwd=123456 ");
String Sqlinsert = "INSERT into class (classname) values (@classname); select Scope_identity ()";
SqlCommand cmd = new SqlCommand (Sqlinsert, con);
SqlParameter par = new SqlParameter ("@classname", "Computer class");
Cmd.parameters.add (PAR);
Con.open ();
int Classid=convert.toint32 (Cmd.executescalar ());
Con.close ();
Next, if you want to insert data into the Student information table, we can use the ClassID value obtained above to insert into the ClassID column of the Student information table.
When you want to insert data into two tables that have a primary foreign key relationship, if the primary key of the primary table is a self increasing, after inserting a data into the main table, we need to get the automatically growing primary key value and insert the data into another table as a foreign key value when inserting it into the other table.
There are three ways to get the values inserted into an identity column in SQL Server: Scope_identity, ident_current, and @ @identity. The function of these three functions is to get the values of the identity columns, but they are different scopes. About the difference between these three explanations for the Help documentation for SQL Server: Ident_current is not limited by scope and session, but is limited by the specified table. Ident_current returns the values generated for a specific table in any session and scope. The scope_identity and @ @identity return the last identity value generated in any table in the current session. However, scope_identity only returns the values inserted into the current scope; @ @identity is not limited to a specific scope. (if two statements are in the same stored procedure, function, or batch, they are in the same scope)