Skip to main content

Posts

Showing posts with the label IDENTITY

Difference between scope_identity (),@@identity, ident_current

Identity is the property in table. Identity column values automatically assigned value whenever new record inserted into a table. Note: A table having only one identity column. In real time scenario whenever new record inserted into record we need to return that Last Identity value to end user, because of end user application need to check the record gets inserted successfully or not. If we want to return the last identity value we have 3 built in statements in SQL SERVER. 1.        scope_identity () 2.        @@identity 3.        ident_current Scope_identity (): This function returns the last identity genarated value in the current session and same currnet scope. @@identity This function returns the last identity geanrated value in the current and regardless of scope. ident_current In this function we need to pass table name as input parameter. ...

Which data type is preferable over identity column?

An Identity column in SQL-SERVER It is automatically inserted the value in identity column whenever new record gets inserted into the table. --Create Student if object_id ( 'Student' ) is null create table Student ( id tinyint identity ( 1 , 1 ) , Name varchar ( 20 ), Marks int ) If we observe above Create table statement the Id column is identity column and its data type is tinyint . We know that tinyint accepts 0 to 255 numbers range. --insert data into Student table. Declare @start int = 1 while ( @start <= 256 ) begin                 insert into Student ( Name , Marks ) values ( 'Rakesh' , 500 ) set @start = @start + 1 end In the above while loop we are trying to insert same record in 256 time. The identity column automatically supplied value. The 1-255 records gets inserted successfully.256 record get an error because of tinyint accepts 0-255 records ...