In this example we will create a function that returns power of given number. Sql already has a function named power for this process. But we will create the function ourselves.

1 2 3 4 5 6 7 8 9 10 |
Create Function MyPower(@num int,@pow int) returns int as Begin Declare @i int = 0,@result int = 1 while(@i<@pow) Begin Set @result = @result * @num Set @i += 1 End return @result End |

After running the above code, you can run the codes below and calculate different number’s power.

**Sample1**

1 |
Select dbo.MyPower(3,4) |

Result : 81

**Sample2**

1 |
Select dbo.MyPower(2,3) |

Result : 8

**Sample3**

1 |
Select dbo.MyPower(5,2) |

Result : 25

**Sample1**

1 |
Select dbo.MyPower(6,4) |

Result : 1296

**CLICK HERE TO SEE MORE EXAMPLE ABOUT SCALAR VALUED FUNCTIONS**