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.


About The Author

Madhivanan,an MSc computer Science graduate from Chennai-India, works as a works as a Lead Subject Matter Expert at a company that simplifies BIG data. He started his career as a developer working with Visual Basic 6.0, SQL Server 2000 and Crystal Report 8. As years went by, he started working more on writing queries in SQL Server. He now has good level of knowledge in SQLServer, Oracle, MySQL and PostgreSQL as well. He is also one of the leading posters at www.sqlteam.com and a moderator at www.sql-server-performance.com. His T-sql blog is at http://beyondrelational.com/blogs/madhivanan

No comments: