Stored Procedure - SQL Server vs MySQL

Continuing my series on how same things can be done differently in SQL Server and MySQL, in this post, we will see how Stored Procedures are supported in both SQL Server and MySQL. We also explore the difference in calling them.

Consider the following code assuming that you have a database named test

SQL Server

Create procedure proc_test
as
select 'Hello World from SQL Server'

GO

The above code creates a stored procedure named proc_test in your database. To execute the procedure, you need to use either EXEC or EXECUTE keywords

EXEC proc_test

This statement will display the result Hello World in SQL Server

MySQL

DELIMITER $$;
DROP PROCEDURE IF EXISTS `test`.`proc_test`$$
CREATE PROCEDURE `test`.`proc_test` ()

BEGIN   
    select 'Hello World from MySQL' as message;

END$$

DELIMITER ;$$


The above code creates a stored procedure named proc_test in the database named test. To execute the procedure, you need to use the keyword CALL

CALL proc_test()

which will display the result Hello World in MySQL.

Note that in MySQL, delimiters are important for creating a stored procedure and the procedure name should be succeed by empty brackets () if there are no parameters.

No comments:

Post a Comment