Skip to main content

Identity Column in SQL Table

Identity Column in the SQL Table:
How it works in SQL table?
How Many Identity column can be created on the SQL table?
How to insert user defined value in the Identity column?
What will be the next value for the Identity column after the user defined value insertion in the table?

Solution:

Drop table #temp; --Drop the temp table if it is already present

Create table #temp --Create the temp table with identity column( A int identity(1,1), --identity column--B int Identity(10,10), -- we can not create two identity column on a able
C varchar(10)
)
--insert values into tableinsert into #temp(c) values ('a') insert into #temp(c) values ('b')insert into #temp(c) values ('c')
Select * from #temp
--set identity insert on ont the table so that we can insert a user defined value in the identity columnSET IDENTITY_INSERT #temp on insert into #temp(A,C) values (5,'d')
--set identity insert off on the table so that it will automatically insert the values in the identity column. the next value of the identity column will be the max value available in the identity column increment value.
SET
IDENTITY_INSERT #temp offinsert
into #temp(c) values ('e')
Select
* from #temp

--@IDENTITY can be used to find out the last inserted value into an identity column on a table.

Select @@IDENTITY

Comments

Popular posts from this blog

SSRS Reports - Space/Gap issue between the objects in SSRS Reports

In Reports where some objects are set to be shown/hide based on some expression/filter condition etc. There Could be some white space issue between the objects on the report( i.e. when few objects are hidden in the report, the white space between the objects will accumulate and displayed as a big gap between the objects on the report). Following are few ways using which we can handle this kind of issue: 1. Remove the Gap between the objects in the report layout. 2. Or put a text box and make the border the same as the background color and apply the same show/hide action on it. 3. If you are using a table/matrix then instead of putting the space between objects, just add one static row above the column header for the table/matrix and make the border color the same background color. below is one approach: The report before any space handling: -----------------------------------------------------------------------------  Report Preview when all the o...

SSRS Reports - When few objects are hidden in the report, the white space between the objects will accumulate and displayed as a big gap between the objects on the report

In Reports where some objects are set to be shown/hide based on some expression/filter condition etc. There Could be some white space issue between the objects on the report( i.e. when few objects are hidden in the report, the white space between the objects will accumulate and displayed as a big gap between the objects on the report). Following are few ways using which we can handle this kind of issue: 1. Remove the Gap between the objects in the report layout. 2. Or put a text box and make the border the same as the background color and apply the same show/hide action on it. 3. If you are using a table/matrix then instead of putting the space between objects, just add one static row above the column header for the table/matrix and make the border color the same background color. below is one approach: The report before any space handling: -----------------------------------------------------------------------------  Report Preview when all the obj...

SSRS Reports - How to Create Static Rows in a Matrix in SSRS?

Today one of my team members asked me about how to create a matrix in which the rows will remain static/constant and the column group will grow as per the data available. SQL Server Reporting Services 1. Create a data set to get the required data for the matrix. 2. Drag a matrix object on the report body. 3. Add the number of required rows to outside the row group  (This is the main trick for creating the static rows in the matrix) 4. Delete the row group detail row from the matrix 5. Add the labels to the row lines headers, add the column group values and data values in the matrix 6. Run the Report and your Matrix is ready with Static Rows! Happy learning!!!