0

This query should return 4 rows but returns 1 with the ID of 62983. Any reason why?

SELECT *
    FROM sims_user_role_maps surm
    JOIN sims_role sr ON surm.role_id = sr.id
    WHERE 
        surm.user_id = 118730
        AND sr.organization_id REGEXP 62978|62981|62982|62983

SP Call

call schools_remove_for_user('f155ec0e-b9ce-11e1-a1fa-001cc4565d26', '62966|62969|62983') 

SP

CREATE PROCEDURE `schools_remove_for_user`(
    user_id NVARCHAR(255),
    school_ids LONGTEXT
)
BEGIN

DECLARE sims_user_id INT DEFAULT NULL;

SELECT u.sims_id 
INTO sims_user_id 
FROM user u 
WHERE u.id = user_id;

SET SQL_SAFE_UPDATES = 0;

DELETE FROM sims_user_role_maps
WHERE 
        user_id = sims_user_id
    AND role_id IN (
                SELECT role_id 
                FROM(
                    SELECT surm.role_id 
                    FROM sims_user_role_maps surm
                    JOIN sims_role sr ON surm.role_id = sr.id
                    WHERE 
                            surm.user_id = sims_user_id
                        AND sr.organization_id REGEXP school_ids) 
                A);

END
2
  • are you sure those other rows exist? 78, 81, 82? And have a user_id = 118730 in the surm table? Commented Jul 16, 2012 at 18:53
  • This is a dynamic string, and yes they do belong to that user ID. I even removed the last ID 62983 and it is still bringing back that only row. Commented Jul 16, 2012 at 18:55

2 Answers 2

1

You did not place the regular expression inside quotes, so | works as bitwise OR.

The correct syntax is AND sr.organization_id REGEXP '62978|62981|62982|62983'. You can also consider sr.organization_id IN (x, y, z) which is better if you are just trying to match specific integers.

Sign up to request clarification or add additional context in comments.

2 Comments

This works directly when I execute the SQL but in a stored proc it doesnt.
@MikeFlynn: Are you sure that the SP doesn't have some other problem?
0

The reason was the user_id in the delete statement. I added the table name as the prefix.

DELETE FROM sims_user_role_maps
WHERE 
        sims_user_role_maps.user_id = sims_user_id
    AND sims_user_role_maps.role_id IN (

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.