Skip to main content

difference between @@IDENTITY, SELECT SCOPE_IDENTITY(),SELECT IDENT_CURRENT

SELECT @@IDENTITY: it’s responsible to returns the last identity value generated for any table in the current session, across all scopes (i.e. global scope).

SELECT SCOPE_IDENTITY(): It’s responsible to returns the last identity value generated for any table in the current session and the current scope(i.e. local scope).
SELECT IDENT_CURRENT(‘table_name’): It’s responsible to returns the last identity value generated for a specific table in any session and any scope (i.e. global scope).
Following example may clear your concept.
CREATE TABLE TBL1
(id INT IDENTITY(1,1))
CREATE TABLE TBL2(
id INT IDENTITY(100,1))
GO

CREATE TRIGGER TGR ON TBL1 FOR INSERT
AS
BEGIN
INSERT TBL2 DEFAULT VALUES
END
GO

INSERT TBL1 DEFAULT VALUES
SELECT @@IDENTITY AS FOR_IDENTITY
SELECT SCOPE_IDENTITY() AS FOR_SCOPR_IDENDITY
SELECT IDENT_CURRENT('TBL1') AS FOR_IDENT_CURRENT_TBL1
SELECT IDENT_CURRENT('TBL2') AS FOR_IDENT_CURRENT_TBL2
GO
DROP TABLE TBL1
DROP TABLE TBL2
GO


Comments

Popular posts from this blog

Bulk Update in sql server 2008..

/*Bulk update query*/ create table Table1 ( ID int identity ( 1 , 1 ), vIsbn varchar ( 20 ), dcPrice decimal ( 18 , 2 ) ) create table Table2 ( ID int identity ( 1 , 1 ), vIsbn varchar ( 20 ), dcPrice decimal ( 18 , 2 ) ) /*Here create two tables, we update Table1's dcPrice column from Table 2 Using Bulk update*/     insert into Table1 ( vIsbn , dcPrice ) values ( '9087657654312' , 0.00 ) insert into Table1 ( vIsbn , dcPrice ) values ( '9989876765454' , 0.00 ) insert into Table2 ( vIsbn , dcPrice ) values ( '9087657654312' , 10.20 ) insert into Table2 ( vIsbn , dcPrice ) values ( '9989876765454' , 20.10 ) select * from Table1 /*here bulk update Query*/ UPDATE Tbl SET dcPrice = Tbl2 . dcPrice FROM Table1 Tbl JOIN Table2 AS Tbl2 ON Tbl . vIsbn = Tbl2 . vIsbn select * from Table1 drop table Table1 dr

TRY/CATCH and error handling in SQL server 2005/2008

SQL Server 2005 provide a strong set of tool to handle error or exception, in privious version it was not easy to handle exceptions. We use TRY/CATCH in SQL Server same as populer languages, We write saperate logic and exception handling code. Syntax: BEGIN TRY -- Your Code or query END TRY BEGIN CATCH -- Handling code END CATCH We write our logic code within TRy bolck and exception code in CATCh block, if nay error occur in logic code it jumps to CATCH  block, resume the execution and transaction is rollback. Functions which is use in CATCH 1- ERROR_NUMBER() Returns error no othe error message. 2- ERROR_SEVERITY() returns the error severity regardless of how many times it is run. 3- ERROR_STATE() Returns the state number of error messages that cause the catch block. 4- ERROR_PROCEDURE() Return the Procedure Name. 5- ERROR_LINE() Return error line number. 6- ERROR_MESSAGE() return error description. Note : If we are calling all above method out side from C

Constraints in sql server

Constraints   - Constraints are the rules enforced on data columns on table. This ensures the accuracy and reliability of the data in the database. Constraints define some conditions that restricts the column to remain true while inserting or updating or deleting data in column. Constraints can be defined in two ways   1) The constraints can be specified immediately after the column definition. This is called column-level definition.   2) The constraints can be specified after all the columns are defined. This is called table-level definition.   Types of Constraints in SQL-Server NOT NULL - This constraint is responsible for a column to confirm that a column cannot have a null value. DEFAULT – This constraint provides a default value when specified non for this column. UNIQUE - This constraint insure that each row for the column has different value. PRIMARY KEY - Primary key constraint is a combination of a NOT NULL constraint and a UNIQUE constra