View Single Post
Old 11-15-2012, 09:11 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,567
Thanks: 62
Thanked 4,057 Times in 4,026 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
OF COURSE you should use a STORED PROCEDURE!

Trivial.

Example:
Code:
mysql> select * from foo;
+-----------+-------+
| viewcount | name  |
+-----------+-------+
|         0 | adam  |
|         0 | bob   |
|         0 | candy |
+-----------+-------+

mysql> delimiter //

mysql> create procedure getAndBumpCount( nm varchar(20) )
    -> begin
    ->     update foo set viewcount = viewcount + 1 where name = nm;
    ->     select * from foo where name = nm;
    -> end;
    -> //

mysql> delimiter ;

mysql> call getAndBumpCount ('candy');
+-----------+-------+
| viewcount | name  |
+-----------+-------+
|         1 | candy |
+-----------+-------+

mysql> call getAndBumpCount ('candy');
+-----------+-------+
| viewcount | name  |
+-----------+-------+
|         2 | candy |
+-----------+-------+

mysql> call getAndBumpCount('adam');
+-----------+------+
| viewcount | name |
+-----------+------+
|         1 | adam |
+-----------+------+

mysql> select * from foo;
+-----------+-------+
| viewcount | name  |
+-----------+-------+
|         1 | adam  |
|         0 | bob   |
|         2 | candy |
+-----------+-------+
How hard is that?

Why do you thing executing via CALL would be a bad thing, FouLu? Most DB experts would tell you that you should do *ALL* work via Stored Procedures and not give the casual DB client permissions to use anything *except* Stored Procs!
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote