Reputation: 521
I have the sql query below. I want to take the results and insert it into another table using the insert below. Is this a good choice for a stored procedure.
It doesn't need to run very often, so no need to attach it to a trigger... I'll probably just write a php script to trigger it via an hourly cron job. (or maybe I can trigger it via command line)
So, am I barking up the wrong tree on this one? Thanks Oh, I'll accept any helpful answer.
SELECT
COUNT(*) AS thecount,
MAX(datetime_acc) AS DATE,
u.created_usr,
@payout:=IF(u.created_usr < '2011-01-24',20,10) AS payout,
level_usr,
@uid_usr:=p.uid_usr,
@affiliate:=u.affiliate_aff,
created_usr,
firstname_usr,
lastname_usr,
contact_aff,
c.id_com
FROM payment_acc p
LEFT JOIN users_usr u ON p.uid_usr = u.id_usr
LEFT JOIN commissions_com c ON c.uid_usr = u.id_usr
LEFT JOIN affiliate_aff a ON a.code_aff = u.affiliate_aff
WHERE p.type_acc = 'monthly payment'
AND affiliate_aff IS NOT NULL
GROUP BY p.uid_usr
HAVING thecount > 1
ORDER BY affiliate_aff
The INSERT:
Insert into commissions_com
date_generated_com,
amount_com,
uid_usr,
code_aff,
status_com
values
(NOW(),
@payout,
@uid_usr,
@code_aff,
'new')
Upvotes: 2
Views: 138
Reputation: 63538
Using stored procedures is a strategic decision; it should not be taken lightly.
MySQL stored procedures are very limited and have a lot of drawbacks:
For me, these are all good reasons not to use stored procedures. They're difficult to write (the language is weak), you have to write code that uses "bad" patterns, and they're difficult to write correctly (hard to debug, it's difficult to know what's happening inside).
Also many people cite some benefits
Upvotes: 1
Reputation: 7169
What "value" are you looking for?
Jeff Atwood has for a long time questioned the use for stored procuedres.
From the mysql documentation:
MySQL 5.0 introduced Stored Procedures which allow us to automate or program our way out of many tasks on directly on the server rather than having to write external scripts to do complex manipulation of data.
As you get used to writing stored Procedures in MySQL 5.0, you will, as with any other programming language, want to generalize your stored procedures as much as possible. The more flexible your stored procedure is, the more tasks it can be used for -- and the less places you have to go searching for that elusive bug that just keeps giving you the wrong result. The day you end up making a copy of a stored procedure just to change a name or two is the day you need to think how tweaking the original procedure can accomplish what you want without breaking old functionality.
From what you have described, your script doesn't sound general or to be used for several tasks.
For the time being then, I would avoid a stored procedure.
Upvotes: 1