Monday 12 August 2013

PRINT TOTAL SALARY using CURSOR

--PRINT TOTAL SALARY using CURSOR 

CREATE TABLE Employee
(
 EmpID int PRIMARY KEY,
 EmpName varchar (50) NOT NULL,
 Salary int NOT NULL,
 Address varchar (200) NOT NULL,
)
GO
INSERT INTO Employee(EmpID,EmpName,Salary,Address) VALUES(1,'Mohan',12000,'Noida')
INSERT INTO Employee(EmpID,EmpName,Salary,Address) VALUES(2,'Pavan',25000,'Delhi')
INSERT INTO Employee(EmpID,EmpName,Salary,Address) VALUES(3,'Amit',22000,'Dehradun')
INSERT INTO Employee(EmpID,EmpName,Salary,Address) VALUES(4,'Sonu',22000,'Noida')
INSERT INTO Employee(EmpID,EmpName,Salary,Address) VALUES(5,'Deepak',28000,'Gurgaon')
GO
SELECT * FROM Employee 
------------------CURSOR-------------------
alter proc usp_emp_totalsal as
begin
set nocount on
declare @id int 
declare @name varchar(50)
declare @Each_row_sal int=0
declare cursortable cursor
for select empID,empname,salary from Employee
open cursortable
fetch next from cursortable into @id,@name,@Each_row_sal
declare @totalsal int=0
while(@@FETCH_STATUS=0)
begin
print @totalsal
set @totalsal=@totalsal+@Each_row_sal
fetch next from cursortable into @id,@name,@Each_row_sal
end
close cursortable
deallocate cursortable
print @totalsal
set nocount off

end

exec usp_emp_totalsal

No comments:

Post a Comment

EMP, DEPT Sample script

/****** Object:  Table [dbo].[DEPT]    Script Date: 19-05-2016 06:58:37 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET A...