What is the difference between scope_identity() and current_identity()? - sql-server

What is the difference between scope_identity() and current_identity()?
Scope_identity and current _identity both are similar and it will return the last identity value generated in the table.
Scope_Identity will return the identity value in table that is currently in scope
is it correct definition for this question?

IDENT_CURRENT is similar to the SQL Server 2000 identity functions SCOPE_IDENTITY and ##IDENTITY. All three functions return last-generated identity values.
However, the scope and session on which last is defined in each of these functions differ:
IDENT_CURRENT returns the last identity value generated for a specific table in any session and any scope.
##IDENTITY returns the last identity value generated for any table in the current session, across all scopes.
SCOPE_IDENTITY returns the last identity value generated for any table in the current session and the current scope.
Source

##IDENTITY
It returns the last identity value generated for any table in the current session, across all scopes.
Let me explain this... suppose we create an insert trigger on table which inserts a row in another table with generate an identity column, then ##IDENTITY returns that identity record which is created by trigger.
SCOPE_IDENTITY
It returns the last identity value generated for any table in the current session and the current scope.
Let me explain this... suppose we create an insert trigger on table which inserts a row in another table with generate an identity column, then SCOPE_IDENTITY result is not affected but if a trigger or a user defined function is affected on the same table that produced the value returns that identity record then SCOPE_IDENTITY returns that identity record which is created by trigger or a user defined function.
IDENT_CURRENT
It returns the last identity value generated for a specific table in any session and any scope.
In other words, we can say it is not affected by scope and session, it only depends on a particular table and returns that table related identity value which is generated in any session or scope.
find here

Related

Issue ##IDENTITY AND IDENT_CURRENT(tableName) SQL SERVER

After INSERT record to the table with an identity column
I try to get identified by the way SELECT ##IDENTITY but it returns NULL,
but I try to use ;
SELECT IDENT_CURRENT ('tableName')
this statement return I'm expected value
You shouldn't use ##Identity, nor should you use ident_current().
The first answers a question that you probably don't want to ask, and the second is unreliable, according to SQL Server expert Aaron Bertrand (or, at least, that was the case back in January 2014, when this article was published).
You should be using the output clause if you're inserting more than one record, or scope_identity() if you're only inserting one.
Note that the output clause doesn't play nice with triggers, and scope_identity() will have problems if your target table have an instead of insert trigger.
For more information, you can read Use the right tool to get identity values back after an insert over on my blog.
IDENT_CURRENT() returns the last inserted identity value for a given table.
SCOPE_IDENTITY() returns the last identity value inserted into an identity column in any table in the current session and current scope.
Probably you execute SCOPE_IDENTITY() in the other session

Why does SQL Server generate a new one identity value even if the insert fails?

The query to create the table with its respective field set as IDENTITY is the following:
CREATE TABLE user (
email varchar(100) primary key
name varchar(30),
pwd varchar(10)
)
Alter table to add IDENTITY field:
ALTER TABLE user ADD id int /*NOT NULL*/ IDENTITY;
The email field to be PRIMARY KEY INDEX will fail if a NULL or DUPLICATED value was set, for example supposed that myemail#domain.com already exists, OK the query fails, but I change the email to anotheremail#domain.com SQL Server generate a new one value for the IDENTITY field based on the query(s) that failed before. My question is why does this happen? (Is this ONLY on SQL Server or other database providers also)
Well, this is clearly documented in "CREATE TABLE (Transact-SQL) IDENTITY (Property)":
Reuse of values - For a given identity property with specific seed/increment, the identity values are not reused by the engine. If a particular insert statement fails or if the insert statement is rolled back then the consumed identity values are lost and will not be generated again. This can result in gaps when the subsequent identity values are generated.
Further along the documentation also answers why and suggests what to do if this is not acceptable:
These restrictions are part of the design in order to improve performance, and because they are acceptable in many common situations. If you cannot use identity values because of these restrictions, create a separate table holding a current value and manage access to the table and number assignment with your application.

Retrieving new rowversion after INSERT using ##DBTS - is it safe?

It is common practice to pick up the newly created IDENTITY of a table using the ##IDENTITY variable after an INSERT.
Is it just equality correct to retrieve the last rowversion value following an UPDATE in a similar manner using the ##DBTS value?
For example:
IF(OBJECT_ID('XXX') IS NOT NULL)
DROP TABLE XXX
GO
CREATE TABLE XXX
(
ID int IDENTITY(1,1) PRIMARY KEY,
Name varchar(64) NOT NULL,
RV rowversion
)
GO
INSERT INTO XXX(Name) VALUES
('Apples'),('Bananas'),('Cranberries'),('Dragon Fruit'),('Eggplant'),('Fig'),('Grape')
GO
SELECT * FROM XXX
GO
UPDATE XXX
SET Name = 'Chocolate' WHERE ID = 3
PRINT ##DBTS
GO
Now is ##DBTS safe from concurrent updates?
If another connection performs insert and updates between the UPDATE and the PRINT, would we end up with the rowversion of the 'other' connection rather than the one from our own update?
According to MSDN ##DBTS
returns the last-used timestamp value of the current database
This means it is not thread safe.
You also should not use ##IDENTITY. ##IDENTITY and SCOPE_IDENTITY return the last identity value generated in any table in the current session. However, SCOPE_IDENTITY returns the value only within the current scope; ##IDENTITY is not limited to a specific scope. ##IDENTITY can for example return the wrong value if a trigger gets executed on a table with an identity column.

Actual inserted row ID

I have created table in my db in this statement
CREATE TABLE tPerson
(
id INT NOT NULL PRIMARY KEY identity(1,1)
, name NVARCHAR(100) not null
, email NVARCHAR(30) not null
)
GO
Now I insert new value with INSERT. My question is how can I get id of current added row? Any idea ??
Assuming SQL server, you should check out this article to gain a good understanding of retrieving identities.
Here's a snippet:
SELECT ##IDENTITY
It returns the last IDENTITY value produced on a
connection, regardless of the table that produced the value, and
regardless of the scope of the statement that produced the value.
##IDENTITY will return the last identity value entered into a table in
your current session. While ##IDENTITY is limited to the current
session, it is not limited to the current scope. If you have a trigger
on a table that causes an identity to be created in another table, you
will get the identity that was created last, even if it was the
trigger that created it.
SELECT SCOPE_IDENTITY()
It returns the last IDENTITY value produced on
a connection and by a statement in the same scope, regardless of the
table that produced the value. SCOPE_IDENTITY(), like ##IDENTITY, will
return the last identity value created in the current session, but it
will also limit it to your current scope as well. In other words, it
will return the last identity value that you explicitly created,
rather than any identity that was created by a trigger or a user
defined function.
SELECT IDENT_CURRENT(‘tablename’)
It returns the last IDENTITY value
produced in a table, regardless of the connection that created the
value, and regardless of the scope of the statement that produced the
value. IDENT_CURRENT is not limited by scope and session; it is
limited to a specified table. IDENT_CURRENT returns the identity value
generated for a specific table in any session and any scope.
It looks like SQL Server, and it that case, just use:
INSERT INTO dbo.tPerson(....) VALUES(.....)
DECLARE #NewID INT
SELECT #NewID = SCOPE_IDENTITY()
SCOPE_IDENTITY returns the last inserted IDENTITY value in this current scope.
Side note: "email" is only 30 characters long!?!? I typically make that the longest column in my table - 200 chars or even more :-)
Use ##IDENTITY or SCOPE_IDENTITY for MS SQL Server :)
Try
SELECT ##IDENTITY AS LastID
after your INSERT
You can also do this through JDBC directly to avoid the need to select, as typically an insert statement will return the number of rows inserted which you may want to validate. Spring supports this through its JdbcTemplate, see here

sql server 2005:is it safe to use ##identity?

i have a procedure in which i am inserting record in employee table.nad getting empid by using ##identity ? when this procedure will be called by more than one user at same time,there can be possibility that it returns identity of some other employee inserted at same time.because there is no lock on identity by system?
--code
--identity on for empid column
insert into employee (name) values ('sahil');
return ##identity
refer sql server 2005:is it safe to use ##identity?
for lock on identity issue
You should be using SCOPE_IDENTITY() instead. However, ##IDENTITY refers to the current connection so other users won't affect you but there are other issues to consider.
More information here.
##identity is not safe to use. If the table has a trigger with an insert to a differnt table with an identity that is the value that will be returned. Never use it to get the idnetity value you just inserted. You may think well I don't have a trigger now, but you never know when one might be added and you can go a long time before realizing that your data is hopelessly messed up.

Resources