Monday, 13 August 2012

Creating Function in SQL Server.





Creating Function in SQL Server.

A block of program which is used to perform specific task, is called Function.



create table emp_salary(id int primary key identity(1,1), current_salary bigint )

insert into emp_salary(current_salary) values(5000)
insert into emp_salary(current_salary) values(6000)
insert into emp_salary(current_salary) values(7000)
insert into emp_salary(current_salary) values(8000)
insert into emp_salary(current_salary) values(8000)
insert into emp_salary(current_salary) values(6000)

Creating a Function for calculate next_month_salary (current_salary_2000)

create function f2(@sal int)
returns int
as
begin
declare @next_month_salary int
set @next_month_salary=@sal+2000
return @next_month_salary
end
Calling Current Month Salary
select Current_salary from emp_salary

Calling function in SQL Server
 select current_salary,dbo.f2(current_salary) as Next_month_salary from emp_salary


                                                                            
                  

                                   

0 comments: