简体   繁体   中英

How to reuse same query code (Not result) in other stored procedures mysql?

I am working on stored proceudes in mysql.

I found that for some of proc i have to write a same portion of sql query, But i need a way to reuse the same query code(not result) in all proc, How i can do that?

Here is my S.proc:

BEGIN
    SELECT date, sum(size_kg) as `total_size`, sum(net_size) as `total_net_size`,sum(bags) as `total_bags` FROM `stock` WHERE date=CURDATE() GROUP By CURDATE();
END

The code before where clause will be same in all proc, So How i can reuse that query portion code (Not result) ?

Since this particular procedure seems to be just a single, straightforward SELECT statement, the easiest thing to make it re-usable from multiple other places would simply be to turn it into a View instead.

If, as you say, the WHERE clause could be different depending on where the code is used, then simply omit the WHERE clause from the View, and let each caller set its own WHERE clause to restrict the data returned by the view to whatever it needs to be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM