SQL Server BasicsWhat is SQL Server database?What is RDBMS?What is Normalization?Why we use Denormalization?What_is_SQL?What is PL/SQL?Difference between SQL and PL/SQLDatabase TableOne to One RelationshipOne to Many RelationshipMany to Many RelationshipMany to One RelationshipString Data TypesNumber Data TypesDate Data TypesOther Data TypesCreate DatabaseDrop DatabaseCreating and Managing Users in SQL ServerCreate TableAlter TableDrop TableConstraints in SQL serverPrimary KeyForeign KeyUnique KeyCandidate KeyComposite KeyDifference between primary key and candidate keyPrimary key and foreign key relationshipSurrogate KeyCascading Referential Integrity ConstraintsSelf Referential Integrity ConstraintsInsert into statementInsert multiple recordsUpdate statementDelete statementTruncate statementDifference between Delete and TruncateAlias in SQL ServerSelect statementSelect DistinctSelect TopSelect IntoNull Functions(ISNULL(),NULLIF(),COALESCE())Sub QueryIdentity ColumnSequence objectDifference between sequence and identity columnSQL Server ClausesWHERE ClauseOrder By ClauseTop N ClauseGroup By ClauseHaving ClauseDifference between Where and HavingSQL Server OperatorsArithmetic OperatorsComparison OperatorsLogical OperatorsBitwise OperatorsAny OperatorsAll OperatorsUnion OperatorsUnion All OperatorsDifference between Union and Union AllIntersect OperatorExcept OperatorDifference between Except and IntersectJoinsInner JoinLeft JoinRight JoinFull JoinSelf JoinCross JoinViewsWhat are views?Create views using SSMSIndexed ViewsComplex ViewsCheck Option in ViewCheck Encryption in ViewSchema Binding Option in ViewRead-only ViewsUpdatable ViewsAdvantages and disadvantages of viewsCreate multiple views on one tableCan we implement index on views?Can we Perform Insert, update, delete operation on views?Stored Procedure and FunctionsWhat are Stored Procedures?Why we use stored procedures?Passing parameters to Stored procedureUser-Defined FunctionsDifference between UDF and Stored procedurePre-Defined Functions@@Indentity and Scope_IndentityNULLIF, ISNULL and COALESCE

@@Indentity and Scope_Indentity?

In SQL Server, @@IDENTITY and SCOPE_IDENTITY() are special functions that are used to retrieve the last generated identity value within the current session or scope. They are commonly used when working with tables that have an identity column, which is a column that automatically generates unique values.

Here's an explanation of each function:

  1. @@IDENTITY:
    • @@IDENTITY is a system function that returns the last identity value generated in the current session across all tables.
    • It is not limited to the current scope or specific table.
    • It returns the last identity value regardless of which table or scope generated it.
    • It is important to note that if there are triggers on the table that insert into another table with an identity column, @@IDENTITY will return the last identity value generated by any insert operation within the session, including those triggered by other tables.
  2. SCOPE_IDENTITY():
    • SCOPE_IDENTITY() is a function that returns the last identity value generated within the current scope (e.g., a stored procedure, trigger, or batch) and by the statement that caused the identity value to be generated.
    • It is more precise and reliable when you specifically want to retrieve the last identity value within the current scope
    • It is not affected by inserts triggered by other tables or triggers.
    • It only returns the last identity value generated by the statement that caused the identity value to be generated. For example, if an INSERT statement triggers multiple rows to be inserted, SCOPE_IDENTITY() will return the identity value of the last inserted row within that statement.

Here's an example to illustrate their usage:


-- Assuming there is a table called "MyTable" with an identity column "ID"

-- Insert a row and retrieve the last identity value using @@IDENTITY
INSERT INTO MyTable (Column1, Column2) VALUES ('Value1', 'Value2')
SELECT @@IDENTITY

-- Insert another row and retrieve the last identity value using SCOPE_IDENTITY()
INSERT INTO MyTable (Column1, Column2) VALUES ('Value3', 'Value4')
SELECT SCOPE_IDENTITY()

In the above example, the first SELECT statement with @@IDENTITY will return the last identity value generated by any insert operation in the current session, including the insert triggered by the second INSERT statement. However, the second SELECT statement with SCOPE_IDENTITY() will only return the last identity value generated by the second INSERT statement itself.