In this article, you will see how you can write the logic of stored procedure to generate prime numbers for any given input.

**Title :**

Given a number N print all the prime number (<=N) separated by comma (, ) using Stored Procedure in MYSQL.

**Example-1 :**

Input : N = 10 Output : 2, 3, 5, 7

**Example-2 :**

Input : N = 20 Output : 2, 3, 5, 7, 11, 13, 17, 19

**Program :**

A program to demonstrate that how you can generate the prime numbers for any given input.

delimiter $$ create procedure getPrime(IN n int, OUT result varchar(200)) Begin declare j, i, flag int; /* Declare variables */ set j:=2; set result:=' '; while(j<n) do /* Loop from 2 to n */ set i:=2; set flag:=0; while(i<=j) do /* Loop from 2 to j */ if(j%i=0)then set flag:=flag+1; end if; set i:=i+1; /* Increment i */ end while; if (flag=1) then set result:=concat(result, j, ', '); /* Concat the prime number with ', ' */ end if ; set j:=j+1; /* Increment j */ end while; End $$

**How to call the procedure :**

To call the procedure used the following query given below.

call getPrime(20, @result); select substr(@result, 1, length(@result)-1); /* To remove last character */

**Output :**

2, 3, 5, 7, 11, 13, 17, 19

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

## Recommended Posts:

- MySQL | Creating stored function
- Difference between Trigger and Procedure in DBMS
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- MySQL | Common MySQL Queries
- SQL | Difference between functions and stored procedures in PL/SQL
- What is Stored Procedures in SQL ?
- Advantages and Disadvantages of Using Stored Procedures - SQL
- Difference between Stored and Derived Attribute
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Prime numbers after prime P with sum S
- Print the nearest prime number formed by adding prime numbers to N
- Check if a prime number can be expressed as sum of two Prime Numbers
- Print prime numbers with prime sum of digits in an array
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Sum of prime numbers without odd prime digits
- Check if a number is Prime, Semi-Prime or Composite for very large numbers
- Permutation of first N positive integers such that prime numbers are at prime indices
- Permutation of first N positive integers such that prime numbers are at prime indices | Set 2

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.