Вы находитесь на странице: 1из 7

Identity Column in SQL

Relational Database Concepts


Mr. Vidya Sagar
M.C.M.,MCP.,O.C.A.

IDENTITY Property Identifier columns can be implemented using the IDENTITY property, which allows the application developer to specify both an identity number for the first row inserted into the table (Identity Seed property) and an increment (Identity Increment property) to be added to the seed to determine successive identity numbers. When inserting values into a table with an identifier column, Microsoft SQL Server 2000 automatically generates the next identity value by adding the increment to the seed.

Note: A table can have only one column defined with the IDENTITY property, and that column must be defined using the decimal, int, numeric, smallint, bigint, or tinyint data type.

IDENT_CURRENT Returns the last identity value generated for a specified table in any session and any scope. Syntax IDENT_CURRENT('table_name') IDENT_INCR Returns the increment value (returned as numeric(@@MAXPRECISION,0)) specified during the creation of an identity column in a table or view that has an identity column. Syntax IDENT_INCR ( 'table_or_view' ) IDENT_SEED Returns the seed value (returned as numeric(@@MAXPRECISION,0)) specified during the creation of an identity column in a table or a view that has an identity column. Syntax IDENT_SEED ( 'table_or_view' )

@@IDENTITY Returns the last-inserted identity value. Syntax @@IDENTITY


SCOPE_IDENTITY Returns the last IDENTITY value inserted into an IDENTITY column in the same scope. A scope is a module -- a stored procedure, trigger, function, or batch. Thus, two statements are in the same scope if they are in the same stored procedure, function, or batch. Syntax SCOPE_IDENTITY( ) IDENT_CURRENT Returns the last identity value generated for a specified table in any session and any scope. Syntax IDENT_CURRENT('table_name')

Example script.
CREATE TABLE employees ( Empno int IDENTITY(1,1), fname varchar (20), lname varchar(30), Sex char(1), others.. ) INSERT employees (fname, lname, Sex, Others) VALUES ('Karin', 'Josephs, F, Others)

SET IDENTITY_INSERT tablename ON


To insert the value manually into identity column.

***

Thank You

Вам также может понравиться