- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can I write a MySQL stored function that calculates the factorial of a given number?
Following is the example of a stored function that can calculate the factorial of a given number −
CREATE FUNCTION factorial (n DECIMAL(3,0)) RETURNS DECIMAL(20,0) DETERMINISTIC BEGIN DECLARE factorial DECIMAL(20,0) DEFAULT 1; DECLARE counter DECIMAL(3,0); SET counter = n; factorial_loop: REPEAT SET factorial = factorial * counter; SET counter = counter - 1; UNTIL counter = 1 END REPEAT; RETURN factorial; END // mysql> Select Factorial(5)// +--------------+ | Factorial(5) | +--------------+ | 120 | +--------------+ 1 row in set (0.27 sec) mysql> Select Factorial(6)// +--------------+ | Factorial(6) | +--------------+ | 720 | +--------------+ 1 row in set (0.00 sec)
- Related Articles
- How to write a MySQL stored function that updates the values in a table?
- How to write a MySQL stored function that inserts values in a table?
- How can I use MySQL OCTET_LENGTH() function to count the number of characters stored in a data column?
- How can we alter a MySQL stored function?
- How can we create MySQL stored procedure to calculate the factorial?
- How can I create a MySQL stored procedure that returns multiple values from a MySQL table?
- How can we create a MySQL stored function that uses the dynamic data from a table?
- Write a Golang program to find the factorial of a given number (Using Recursion)
- How can we delete a MySQL stored function from the database?
- How can we write MySQL handler in a stored procedure?
- How can we see the source code of a particular MySQL stored function?
- How can we use a MySQL stored function in a database query?
- How can we write MySQL handler, in a stored procedure, that sets the particular value of a variable and continues the execution?
- How can I find the number of arguments of a Python function?
- How can we write MySQL handler, in a stored procedure, that use SQLSTATE for default MySQL error and exit the execution?

Advertisements