Create a MySQL stored procedure that generates five random numbers?

MySQLMySQLi Database

With the help of the following query we can create a stored procedure to generate five random numbers −

mysql> DELIMITER //
mysql> DROP PROCEDURE IF EXISTS RandomNumbers;
    -> CREATE PROCEDURE RandomNumbers()
    -> BEGIN
    -> SET @i = 0;
    -> REPEAT
    -> SELECT RAND() AS 'Random Number';
    -> SET @i = @i + 1;
    -> UNTIL @i >=5 END REPEAT;
    -> END
    -> //
Query OK, 0 rows affected (0.16 sec)

Query OK, 0 rows affected (0.16 sec)

Now, invoke the procedure to get the result −

mysql> DELIMITER ;
mysql> CALL RandomNumbers();
+---------------------+
| Random Number       |
+---------------------+
| 0.25968261739209536 |
+---------------------+
1 row in set (0.00 sec)

+--------------------+
| Random Number |
+--------------------+
| 0.1659662734400167 |
+--------------------+
1 row in set (0.01 sec)

+---------------------+
| Random Number       |
+---------------------+
| 0.05078354575744229 |
+---------------------+
1 row in set (0.01 sec)

+--------------------+
| Random Number      |
+--------------------+
| 0.7560189392008064 |
+--------------------+
1 row in set (0.04 sec)

+------------------+
| Random Number    |
+------------------+
| 0.62774408946535 |
+------------------+
1 row in set (0.04 sec)

Query OK, 0 rows affected (0.05 sec)
raja
Published on 21-Feb-2018 12:23:05
Advertisements