In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the example analysis of MySQL calling stored procedures and functions, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article.
Stored procedures and functions can be called in many ways. The stored procedure must be called using the call statement, and the stored procedure is related to the database, and you need to specify the database name if you want to execute the stored procedure in another database. For example, call dbname.procname. Stored functions are called in the same way as predefined functions in MySQL.
1. Call the stored procedure
The stored procedure is called through a call statement, and the syntax is as follows:
Call sp_name ([parameter [,...])
The call statement calls a stored procedure that was previously created with create procedure, where sp_name is the name of the stored procedure and parameter is the parameter of the stored procedure.
[example 1] define a stored procedure named CountProcl, and then call the stored procedure.
Define stored procedures:
Mysql > delimiter / / mysql > create procedure CountProcl (in sid int, out num int)-> begin-> select count (*) into num from fruits where s_id = sid;-> end / / Query OK, 0 rows affected (0.06 sec) mysql > delimiter
Call the stored procedure:
Mysql > call CountProcl (101 sec); Query OK, 1 row affected (0.08 sec)
View the returned results:
Mysql > select @ num;+-+ | @ num | +-+ | 3 | +-+ 1 row in set (0.00 sec)
The stored procedure returns the fruit type provided by the fruit vendor of the specified s_id=101. The return value is stored in the num variable, viewed using select, and the result is 3.
2. Call the storage function
In MySQL, stored functions are used in the same way as internal functions in MySQL. That is, the user-defined storage function is the same as the MySQL internal function.
[example 2] define the storage function CountProc2, and then call the function as follows:
Mysql > delimiter / / mysql > create function CountProc2 (sid int)-> returns int-> begin-> return (select count (*) from fruits where s_id = sid);-> end//Query OK, 0 rows affected (0.06 sec) mysql > delimiter
Call the storage function:
Mysql > delimiter; mysql > select CountProc2; +-+ | CountProc2 (101) | +-+ | 3 | +-+ 1 row in set (0.05sec)
As you can see, this example is the same as the result returned in the previous example, and although the definitions of stored functions and stored procedures are slightly different, the same functionality can be achieved.
Thank you for reading this article carefully. I hope the article "sample Analysis of MySQL calling stored procedures and functions" shared by the editor will be helpful to you. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
2052_chs_lp\ x64\ setup\ x64 extract sqlcli.exe of CD
© 2024 shulou.com SLNews company. All rights reserved.