PRINT ROW NUMBERS WITH ROWS IN A TABLE IN SQL SERVER — USE OF ROW_NUMBER()

CREATE TABLE EMPLOYEE
(
id INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
EMPNAME VARCHAR(100),
SALARY DECIMAL
)

SELECT * FROM EMPLOYEE

INSERT INTO EMPLOYEE
SELECT ‘Pawan’ , 180000
UNION ALL
SELECT ‘Gaurav’ , 80000
UNION ALL
SELECT ‘Saurabh’ , 80000
UNION ALL
SELECT ‘Pawan’ , 830000
UNION ALL
SELECT ‘Neelam’ , 80000
UNION ALL
SELECT ‘Pawan’ , 80000
UNION ALL
SELECT ‘Sharlee’ , 820000
UNION ALL
SELECT ‘Pawan’ , 80000
UNION ALL
SELECT ‘Rahul’ , 980000

–Check data is inserted.
SELECT * FROM EMPLOYEE

–USE OF ROW_NUMBER()
SELECT id , EMPNAME , SALARY , ROW_NUMBER() OVER ( ORDER BY id ) ROWNUMBER FROM EMPLOYEE

Pawan Kumar
Pawankkmr@hotmail.com

Advertisements