Posts

Showing posts with the label SQL SERVER MANAGEMENT STUDIO

SCOPE_IDENTITY() Vs @@ Identity Vs IDENT_CURRENT(tableName) in Sql Server

Image
Hi there, in this blog post i am going to discuss the basic differences between SCOPE_IDENTITY(), @@Identity and IDENT_CURRENT('tableName').                 Basically all these three are used to retrieve the last inserted identity value in different session and  different scope. Let's try to understand it in detail. SCOPE_IDENTITY() :   It returns the value of identity last inserted within same session and same scope. @@Identity : It is a global variable which returns the value of identity last inserted within same session but in any scope. IDENT_CURRENT('tableName') : It returns the value of identity last inserted in any session and in any scope. Now, i am pretty sure you are wondering about what this session and scope really menu in the above mentioned points.   So, let me clear the same with an example. First we need to create two different but simple table with having the following structure. create table tableA ( id int identity(1,1)

SQL Server Identity with different possible operation on it.

Image
Hello folks,       I dentity column is very common in database design when we want to add a column as id and auto increment by itself. Some time this same feature is known as auto increment in different database(e.g. MYSQL).  Basically id column is if set as identity(auto increment), is automatically managed and increased by itself when new record(s) are inserted. So, get started with creating a new table in sql server with identity column. Either you can use sql query for this, CREATE TABLE tbl_person ( id INT IDENTITY (1, 1) PRIMARY KEY, name VARCHAR (255) NOT NULL, ); or you can use sql server management studio tool for this. You can set identity option for a column as shown in above screenshot. Here, you can set initial seed(first parameter in sql query) and increment seed (second parameter in sql query). On Successfully creation of table if we try to insert identity column column field explicitly the MSSQL server throws an error stating