MySQL LOOP Statement in Stored Procedure

Abhinanda Shri
Updated on 22-Jun-2020 06:39:54

545 Views

MySQL provides us a LOOP statement that executes a block of code repeatedly along with an added flexibility of using a loop label. We have the following two statements that allow us to control the loop −LEAVE statementIt allows us to exit the loop immediately without waiting for checking the condition.Iterate statementIt allows us to skip the entire code under it and start a new iteration.To demonstrate the use of LOOP statement with stored procedures, the following is a stored procedure which constructs a string with even numbers like 2, 4, 6, 8 etc. −mysql> Delimiter // mysql> CREATE PROCEDURE LOOP_loop() ... Read More

Get Output Using MySQL IN Function for Column Comparison

Monica Mona
Updated on 22-Jun-2020 06:38:53

131 Views

In this scenario, we need to use the name of the column as ‘Expression’ which will then be compared with the values in the list. If a column has value/s matched within the list,  the output would be produced. For understanding it, consider the example from employee table having the following data −mysql> Select * from Employee; +----+--------+--------+ | ID | Name   | Salary | +----+--------+--------+ | 1  | Gaurav | 50000  | | 2  | Rahul  | 20000  | | 3  | Advik  | 25000  | | 4  | Aarav  | 65000  | | 5  | Ram    | 20000  | ... Read More

Use MySQL IN Function to Compare Row Constructors

Anjana
Updated on 22-Jun-2020 06:38:09

210 Views

We can also use IN() function to compare row constructors. Consider the following example to make it clearer −mysql> Select (10,2) IN ((5,10),(10,2),(2,10),(100,100)); +--------------------------------------------+ | (10,2) IN ((5,10),(10,2),(2,10),(100,100)) | +--------------------------------------------+ | 1                                          | +--------------------------------------------+ 1 row in set (0.00 sec) mysql> Select (10,2) IN ((5,10),(2,10),(100,100)); +-------------------------------------+ | (10,2) IN ((5,10),(2,10),(100,100)) | +-------------------------------------+ | 0                                   | +-------------------------------------+ 1 row in set (0.00 sec)

Handle Result Set Inside MySQL Stored Procedure

Smita Kapse
Updated on 22-Jun-2020 06:37:40

1K+ Views

We can use a cursor to handle a result set inside a stored procedure. Basically a cursor allows us to iterate a set of rows returned by a query and process each row accordingly.To demonstrate the use of CURSOR within MySQL stored procedure, we are creating the following stored procedure which is based on the values, as shown below, of the table named ‘student_info’ −mysql> Select * from student_info; +-----+---------+----------+------------+ | id  | Name    | Address  | Subject    | +-----+---------+----------+------------+ | 101 | YashPal | Amritsar | History    | | 105 | Gaurav  | Jaipur   | Literature ... Read More

When MySQL IN Function Returns NULL

Arjun Thakur
Updated on 22-Jun-2020 06:36:43

377 Views

Following are the two cases when MySQL IN() function returns NULL as result −Case-1 − When expression on  left side is NULL IN() function will return NULL if the expression on the left side is NULL. Following example will demonstrate it −mysql> Select NULL IN (1, 2, 3, 4, 10); +----------------------+ | NULL IN (1, 2, 3, 4, 10) | +----------------------+ |       NULL           | +----------------------+ 1 row in set (0.00 sec)Case-2 − When one of expression in the list is NULL and no match is foundIN() function will return NULL if it does ... Read More

Declare a Handler While Handling Errors in MySQL

Nishtha Thakur
Updated on 22-Jun-2020 06:33:33

673 Views

It is very important to handle the errors by throwing a proper error message. MySQL provides a handler to handle the error. We can declare a handler with the help of the following syntax −Syntax of handlerDECLARE handler_action FOR condition_value statement;The above syntax shows that we need to use DECLARE HANDLER statement to declare a handler. If a condition whose value matches the condition_value then MySQL will execute the statement and continue or exit the current code block based on the action. Followings are the three major things in the above syntax −Handler_action is of two types and can accept one ... Read More

Write MySQL Handler in a Stored Procedure

Srinivas Gorla
Updated on 22-Jun-2020 06:29:16

440 Views

Whenever an exception occurred in MySQL stored procedure, it is very important to handle it by throwing a proper error message. Suppose, if we do not handle the exception, there would be a chance to fail application with that certain exception in a stored procedure. MySQL provides a handler to handle the exception in the stored procedure. Followings are the four kinds of MySQL handlers which can be used in a stored procedure −DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SELECT 'got an error';The above handler will throw an error message and continues the execution.DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET got_error=1;The above ... Read More

MySQL REPLACE Function with WHERE Clause

Sharon Christine
Updated on 22-Jun-2020 06:25:31

3K+ Views

As we know that WHERE clause is used to put condition/s in MySQL query and MySQL returns result set based on those conditions. Similarly when we use REPLACE() function with WHERE clause, the result set will depend upon the conditions provided. Following is an example by using data from the ‘Student’ table in which REPLACE() function replaces the records of a column ‘Name’ in which the value of column ‘Subject’ is ‘Computers’.Examplemysql> Select Name, REPLACE(Name, 'G', 'S') from student Where Subject = 'Computers'; +--------+------------------------+ | Name   | REPLACE(Name, 'G', 'S') | +--------+------------------------+ | Gaurav | Saurav       ... Read More

Fetch MySQL SET Column as List of Integer Offset

Monica Mona
Updated on 22-Jun-2020 06:25:01

441 Views

We can fetch the MySQL SET column values as a list of integer offset with the help of the MAKE_SET() function. To make it understand, we are creating a table named ‘set_testing’ as follows −mysql> Create table set_testing( id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, table SET('ABC', 'ABD', 'GHF') NOT NULL); Query OK, 0 rows affected (0.08 sec) mysql> Insert into set_testing (table) values('1'); Query OK, 1 row affected (0.06 sec) mysql> Insert into set_testing (table) values('2'); Query OK, 1 row affected (0.06 sec) mysql> Insert into set_testing (table) values('3'); Query OK, 1 row affected (0.02 ... Read More

MySQL Query to Show Equality Condition

Kumar Varma
Updated on 22-Jun-2020 06:24:12

133 Views

The binary equality operators compare their operands for strict equality or inequality. In MySQL, the equal-to-operator (=) returns 1 if both the operands have the same value otherwise returns 0. Following MySQL query show an equality condition −mysql> Select tender_value From estimated_cost WHERE id = 3;The above query shows an equality condition because the column id equates to the integer value.mysql> Select tender_value From estimated_cost1 WHERE Name_company = 'Chd Ltd.';The above query shows an equality condition because column Name_company equates to the string value.

Advertisements