Categories
MySQL

MySQL CASE Statement

MySQL has an alternative of IF statement i.e. MySQL CASE statement by which you can check conditional statement. Besides the IF statement in MySQL, this provides an alternative conditional statement i.e. called CASE. The MySQL CASE statement makes the programming code more readable and efficient. You can use the simple CASE statement to check the […]

Categories
MySQL

MySQL Query Functions

There are so many MySQL functions that commonly used to minimize the statements like string operation functions, date time functions, aggregate functions etc. MySQL LAST_INSERT_ID To get the last generated sequence number of the last inserted row. SELECT LAST_INSERT_ID(); MySQL CAST() To convert a value of any type into a value with a your own […]

Categories
MySQL

IF statement in MySQL stored procedures

If statement allows you to execute a set of SQL statements based on condition. If certain condition returns true then statement executes, otherwise not. Example: DELIMITER $$ CREATE PROCEDURE MyifCond() BEGIN DECLARE x  INT; SET x = 1; SET y = 1; WHILE x  <= 10 DO SET  x = x + 1; IF x […]

Categories
MySQL

Cursor in MySQL stored procedures

Cursor allows you to iterate a group of rows fetched by a query and process each row. It is read only, non-scrollable and asensitive. Declare a cursor: DECLARE <cursor_title> CURSOR FOR SELECT_statement; Open a cursor: OPEN <cursor_title>; Fetch cursor: FETCH <cursor_title> INTO variables list; Close cursor: CLOSE <cursor_title>;

Categories
MySQL

Loop in MySQL stored procedures

MySQL allows you to loop statement for executing a block repeatedly based on a condition. These are like WHILE, REPEAT and LOOP. WHILE LOOP: It checks the expression at the beginning of each iteration. Example: DELIMITER $$ CREATE PROCEDURE MyWhileLoop() BEGIN DECLARE x  INT; SET x = 1; WHILE x  <= 10 DO SET  x […]

Categories
MySQL

Listing stored procedures in MySQL

In MySQL you can list or show all stored procedures and manage more efficiently. Display source code of stored procedure: Use SHOW CREATE PROCEDURE statement to display source code of a particular stored procedure. SHOW CREATE PROCEDURE GetAllUsers; Display characteristics of stored procedure: Use SHOW PROCEDURE STATUS to list all stored procedures. SHOW PROCEDURE STATUS […]

Categories
MySQL

MySQL stored procedure parameters and variables

All the stored procedure needs parameters to make more flexible. It has one of these three modes i.e. IN, OUT or INOUT. IN: Default mode i.e. calling program has to pass an argument to the stored procedures and value of IN parameter is changed inside of stored procedure even after stored procedure ends, the values […]