MySQL stored procedure, handling multiple cursors and query results

前端 未结 5 1367

How can I use two cursors in the same routine? If I remove the second cursor declaration and fetch loop everthing works fine. The routine is used for adding a friend in my webap

5条回答
  •  鱼传尺愫
    2021-02-14 08:54

    Wow, i don't know what to say, please go and read about and learn sql a little, no offense but this is amongst the worst SQL i've ever seem.

    SQL is a set based language, cursors, in general, are bad, there are situations when they are usefull, but they are fairly rare. Your use of cursors here is totally inappropriate.

    Your logic in the second cursor is also flawed since it will select any record which inludes the friend, not just the required friendship.

    If you wanted to fix it you could try giving the second cursor a differant name, but preferably start over.

    Set a compound PK or unique constraint on users_friends, then you don't have to worry about checking for a relationship, then try something like this.

    INSERT INTO users_friends 
    SELECT 
        @inUserId, 
        users.user_id
    FROM 
        users
    WHERE
        email = @inFriendEmail
    

提交回复
热议问题