1)Create Identity Column in a Table
Create Table Emp_Master(Emp_ID int Identity(1,1),Emp_Name Varchar(100))
Identity(1,1) In this first 1 is Identity Seed and second 1 is identity Increment.
Identity Seed : Exposes the Initial row value for an identity column.
Identity Increment: Exposes the value added to the maximum existing row identity value when generating the next identity value.
2)How to find the Identity value of a table
IDENT_CURRENT('Tablename')
select * from Emp_Master
Emp_ID Emp_Name
1 Job
2 Joby
3 Jijo
4 Jojo
5 Jose
6 John
Select IDENT_CURRENT ('Emp_Master') As Value
Value
6
3)How to Reseed the identity value
DBCC CheckIdent(Tablename,Reseed, your desired value)
If we delete data from Emp_Master table, our seed value will not to set back. In that purpose we need to reseed the identity value.
select * from Emp_Master
Emp_ID Emp_Name
1 Job
2 Joby
3 Jijo
4 Jojo
In this example we deleted empid 5 and 6. Now our current identity value will be 6
Select IDENT_CURRENT ('Emp_Master') As Value
Value
6
And if we insert another record into Emp_Master table  the value of Emp_ID will be 7 not be 5
select * from Emp_Master
Emp_ID Emp_Name
1 Job
2 Joby
3 Jijo
4 Jojo
7 John
In this case we need to Reseed the identity column to 4. we deleted where empid is 7
Emp_ID Emp_Name
1 Job
2 Joby
3 Jijo
4 Jojo
DBCC CheckIdent(Emp_Master,Reseed,4)
Now our seed value is 4 . And if we insert empid into emp_master table the value of Emp_id will be 5.
Select IDENT_CURRENT ('Emp_Master') As Value
Value
4
Emp_ID Emp_Name
1 Job
2 Joby
3 Jijo
4 Jojo
5 John
Create Table Emp_Master(Emp_ID int Identity(1,1),Emp_Name Varchar(100))
Identity(1,1) In this first 1 is Identity Seed and second 1 is identity Increment.
Identity Seed : Exposes the Initial row value for an identity column.
Identity Increment: Exposes the value added to the maximum existing row identity value when generating the next identity value.
2)How to find the Identity value of a table
IDENT_CURRENT('Tablename')
select * from Emp_Master
Emp_ID Emp_Name
1 Job
2 Joby
3 Jijo
4 Jojo
5 Jose
6 John
Select IDENT_CURRENT ('Emp_Master') As Value
Value
6
3)How to Reseed the identity value
DBCC CheckIdent(Tablename,Reseed, your desired value)
If we delete data from Emp_Master table, our seed value will not to set back. In that purpose we need to reseed the identity value.
select * from Emp_Master
Emp_ID Emp_Name
1 Job
2 Joby
3 Jijo
4 Jojo
In this example we deleted empid 5 and 6. Now our current identity value will be 6
Select IDENT_CURRENT ('Emp_Master') As Value
Value
6
select * from Emp_Master
Emp_ID Emp_Name
1 Job
2 Joby
3 Jijo
4 Jojo
7 John
In this case we need to Reseed the identity column to 4. we deleted where empid is 7
Emp_ID Emp_Name
1 Job
2 Joby
3 Jijo
4 Jojo
DBCC CheckIdent(Emp_Master,Reseed,4)
Now our seed value is 4 . And if we insert empid into emp_master table the value of Emp_id will be 5.
Select IDENT_CURRENT ('Emp_Master') As Value
Value
4
Emp_ID Emp_Name
1 Job
2 Joby
3 Jijo
4 Jojo
5 John
No comments:
Post a Comment