Saturday, 21 July 2012

How to create function in Sql Server


creating database test 

 create database test

 use test

create function ODD_EVEN for checking no is even or odd in sql server

create function  ODD_EVEN(@id int) returns varchar(50)

as

begin

      declare @type varchar(50)=null

      if @id%2=0

            set @type='No is EVEN'

 else

            set @type='ODD'

            return @type

end


/execute function  for 34 

SELECT DBO.odd_even(34)


Result: No is EVEN


create function  fact  for  factorial for any no like no 4  in sql server

alter function fact(@no int) returns int
as

begin

declare @a int

set @a=1

while @no > 0

begin

      set @a=@a*@no

      set @no=@no-1

      end

return @a

end


execute function  for 4 

select dbo.fact(4)








0 comments:

Post a Comment