Recursively call itself : Recursive function « Procedure Function « SQL Server / T-SQL Tutorial

SQL Server / T-SQL Tutorial
1. Query
2. Insert Delete Update
3. Table
4. Table Join
5. Data Types
6. Set Operations
7. Constraints
8. Subquery
9. Aggregate Functions
10. Date Functions
11. Math Functions
12. String Functions
13. Data Convert Functions
14. Analytical Functions
15. Sequence Indentity
16. View
17. Index
18. Cursor
19. Database
20. Transact SQL
21. Procedure Function
22. Trigger
23. Transaction
24. XML
25. System Functions
26. System Settings
27. System Tables Views
28. User Role
29. CLR
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
SQL Server / T-SQL Tutorial » Procedure Function » Recursive function 
21. 3. 1. Recursively call itself
2CREATE PROC factorial @param1 decimal(38,0), @answer decimal(38,0output
3> AS
4> DECLARE @one_less decimal(38,0), @status int
5>
6> IF (@param1 < OR @param1 > 32)
7>     BEGIN
8>         -- Illegal parameter value. Must be between and 32.
9>         RETURN -1
10>     END
11>
12> IF (@param1=or @param1=1)
13>     SET @answer=1
14> ELSE
15>     BEGIN
16>         SET @one_less=@param1 - 1
17>         EXEC @status=factorial @one_less, @answer output
18>         
19>         IF (@status= -1)
20>             BEGIN
21>                 RETURN -1
22>             END
23>
24>         SET @answer=@answer * @param1
25>
26>         IF (@@ERROR <> 0)
27>             RETURN -1
28>     END
29>
30> RETURN  0
31> GO
1>
2>
3> DECLARE @answer decimal(38,0), @param int
4> SET @param=0
5> WHILE (@param <= 32BEGIN
6>     EXEC  factorial  @param, @answer output
7>     IF (@answer= -1)
8>         BEGIN
9>             RAISERROR('Error executing factorial procedure.', 16, -1)
10>             RETURN
11>         END
12>     PRINT CONVERT(varchar, @param'! ' + CONVERT(varchar(50), @answer)
13>     SET @param=@param + 1
14END
15> GO
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
11! = 39916800
12! = 479001600
13! = 6227020800
14! = 87178291200
15! = 1307674368000
16! = 20922789888000
17! = 355687428096000
18! = 6402373705728000
19! = 121645100408832000
20! = 2432902008176640000
21! = 51090942171709440000
22! = 1124000727777607680000
23! = 25852016738884976640000
24! = 620448401733239439360000
25! = 15511210043330985984000000
26! = 403291461126605635584000000
27! = 10888869450418352160768000000
28! = 304888344611713860501504000000
29! = 8841761993739701954543616000000
30! = 265252859812191058636308480000000
31! = 8222838654177922817725562880000000
32! = 263130836933693530167218012160000000
1>
2> drop PROC factorial;
3> GO
1>
21. 3. Recursive function
21. 3. 1. Recursively call itself
21. 3. 2. iterative solution does not have the restriction 32 nesting levels
21. 3. 3. Implementing the fibonacci() User-Defined Function with Recursion
21. 3. 4. Implementing the fibonacci2() User-Defined Function with a Loop
21. 3. 5. Nesting Stored Procedures
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.