Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are missing the <code>CREATE</code> from the start of your stored procedure, by adding this I was able to get your procedure working on <a href="http://sqlfiddle.com/#!2/8588b/1" rel="nofollow">SQL Fiddle</a> <em>(Note the 4th result set has one row of paid updated)</em></p> <p>To test I created the following data:</p> <pre><code>CREATE TABLE location_byhits (Paid INT, IpAddress INT); CREATE TABLE Users (ID INT); CREATE TABLE Subscription(ItemID INT, UsersID INT); CREATE TABLE Item (ID INT, Item_TypeID INT); // INSERT location_byhits VALUES (0, '192.168.0.0'), (0, '192.168.0.1'); INSERT Users VALUES (1), (2); INSERT Subscription (1, 1), (2, 2); INSERT Item (1, 1), (2, 6); </code></pre> <p>Then called the procedure 4 times with various parameters.</p> <pre><code>CALL PaidUser ('192.168.0.2', 1); CALL PaidUser ('192.168.0.0', 3); CALL PaidUser ('192.168.0.1', 2); CALL PaidUser ('192.168.0.1', 1); </code></pre> <ul> <li>The first should not update as there is no IP Match.</li> <li>The Second should not update as there is no user match.</li> <li>The third should not update because although there is a user and an IP match, user 2 has no items associated that don't have an Item_TypeID of 6.</li> <li>The fourth should update because there is both a user match and an ip match, and the user has associated items that have and item type id other than 6.</li> </ul> <p>I would be inclined to rewrite your stored procedure as follows though:</p> <pre><code>CREATE PROCEDURE `PaidUser`(in IpId VARCHAR(45),in UId INT(20)) BEGIN UPDATE location_byhits SET Paid = 1 WHERE location_byhits.IpAddress = IpId AND EXISTS ( SELECT 1 FROM Users u INNER JOIN Subscription s ON u.ID = s.usersID INNER JOIN Item i ON i.ID = s.ItemID WHERE i.Item_TypeID != 6 AND u.ID = UId ); END </code></pre> <p>If you do the check before the update, although it is very unlikely, it is not impossible that in between starting the check and doing the update the data is changed by another session. This could result in unexpected behaviour. If you do it all in a single statement you avoid concurrency issues.</p> <p><strong><a href="http://sqlfiddle.com/#!2/cc1c8/7" rel="nofollow">Example on SQL Fiddle</a></strong></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload