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)
 factorial of 2 numbers ?

Author  Topic 

ganatra.neha
Starting Member

30 Posts

Posted - 2007-10-28 : 02:46:06
I need to calcuate factorial of 2 numbers simultaneously

My code is as follows

Use AdventureWorks
Go
Alter Procedure Factorial
@Number decimal (38,0),
@Number2 decimal (38,0),
@factorial_1 decimal(38,0)output,
@factorial_2 decimal(38,0)output
as
Declare @tempno1 decimal (38,0)
Declare @tempno2 decimal (38,0)
IF @number<2
SET @factorial_1=1
ELSE
BEGIN
SET @tempno1 = @number-1
EXEC factorial @tempno1, @factorial_1 OUT , -- Recursive call
IF (@factorial_1=-1) RETURN(-1) -- Got an error, return
SET @factorial_1=@factorial_1*@number
IF (@@ERROR<>0) RETURN(-1) -- Got an error, return
END

IF @number2<2
SET @factorial_2 =1
ELSE
BEGIN
SET @tempno2 = @number2 -1
EXEC factorial @tempno2, @factorial_2 OUT , -- Recursive call
IF (@factorial_2=-1) RETURN(-1) -- Got an error, return
SET @factorial_2=@factorial_2*@number2
IF (@@ERROR<>0) RETURN(-1) -- Got an error, return
END


RETURN(0)
GO

I am getting follwing error
Msg 156, Level 15, State 1, Procedure Factorial, Line 15
Incorrect syntax near the keyword 'IF'.
Msg 156, Level 15, State 1, Procedure Factorial, Line 26
Incorrect syntax near the keyword 'IF'

PLease anyone can let me know why i cannot execute code

Kristen
Test

22859 Posts

Posted - 2007-10-28 : 03:07:21
You've got an extra comma on the end of the line (in two places)

The error says "near the keyword 'IF'" - so that's where you need to look, but such syntax errors can be either before or after the place that SQL Server detects them

Kristen
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-10-28 : 15:14:50
There are faster ways to calculate factorials without using recursive calls.
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=67125&SearchTerms=factorial



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -