Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 IF condition

Author  Topic 

karthickbabu
Posting Yak Master

151 Posts

Posted - 2008-01-08 : 06:25:09
I write the Function for Factorial

i use like these
IF (@FactorialFor BETWEEN 0 AND 12 )

IF (@FactorialFor >= 0 AND @FactorialFor < 13 )

But if i give negative value it returns 1
How can handle error in functions

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-08 : 07:15:50
You can use another condition to check for this like

IF (@FactorialFor < 0 )


and write appreopriate code for it.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-01-08 : 07:36:23
Use this function


Create function dbo.factorial(@n int)
returns float
as
begin
declare @result float
set @result=1
if @n>0
begin
select @result=@result*number from
(
select number from master..spt_values
where type='p' and number between 1 and @n
) as t

end
return @result
end

GO


and call as

select dbo.factorial(-1),dbo.factorial(6)


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

karthickbabu
Posting Yak Master

151 Posts

Posted - 2008-01-08 : 07:43:50
Sorry ya.. its simple syntax error...I solve this problem,
Anyway thanks for your reply... Mr.Madhi i got some more ideas from your code
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-01-08 : 08:24:22
quote:
Originally posted by karthickbabu

Sorry ya.. its simple syntax error...I solve this problem,
Anyway thanks for your reply... Mr.Madhi i got some more ideas from your code


Well. If you want to create a function to find factorial, then use the function I posted

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -