I have the following code that works:
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE user_id int(11);
DECLARE cur1 CURSOR FOR SELECT id FROM users;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
# drop and re-create user_rank TABLE
DROP TABLE IF EXISTS user_rank;
CREATE TABLE `user_rank` (
`id` int(11) UNSIGNED NOT NULL,
`has_hobbies` int(3) DEFAULT 0,
`passed_test` int(3) DEFAULT 0,
`has_picture` int(3) DEFAULT 0,
`won_a_job` int(3) DEFAULT 0,
`is_prolancer` int(3) DEFAULT 0,
`is_verified` int(3) DEFAULT 0,
`has_portfolio` int(3) DEFAULT 0,
`has_likes` int(3) DEFAULT 0,
`has_disputes` int(3) DEFAULT 0,
`has-earnings` int(3) DEFAULT 0,
`has_feebacks` int(3) DEFAULT 0,
`has_invitations` int(3) DEFAULT 0,
`has_views` int(3) DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `user_rank`
ADD PRIMARY KEY (`id`);
ALTER TABLE `user_rank`
MODIFY `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO user_id;
IF done THEN
LEAVE read_loop;
END IF;
INSERT INTO user_rank (id) values (user_id);
END LOOP;
CLOSE cur1;
END
it loops inside the table users using a cursor and copy all the user ids into the table user_rank. I have a function called "hasUserPassedTest" defined in my database that given a user id return 10 or 0. I would like to call the function in the loop above and insert it int the user_rank table but the following code does not work:
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE user_id int(11);
DECLARE cur1 CURSOR FOR SELECT id FROM users;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
# drop and re-create user_rank TABLE
DROP TABLE IF EXISTS user_rank;
CREATE TABLE `user_rank` (
`id` int(11) UNSIGNED NOT NULL,
`has_hobbies` int(3) DEFAULT 0,
`passed_test` int(3) DEFAULT 0,
`has_picture` int(3) DEFAULT 0,
`won_a_job` int(3) DEFAULT 0,
`is_prolancer` int(3) DEFAULT 0,
`is_verified` int(3) DEFAULT 0,
`has_portfolio` int(3) DEFAULT 0,
`has_likes` int(3) DEFAULT 0,
`has_disputes` int(3) DEFAULT 0,
`has-earnings` int(3) DEFAULT 0,
`has_feebacks` int(3) DEFAULT 0,
`has_invitations` int(3) DEFAULT 0,
`has_views` int(3) DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `user_rank`
ADD PRIMARY KEY (`id`);
ALTER TABLE `user_rank`
MODIFY `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO user_id;
IF done THEN
LEAVE read_loop;
END IF;
INSERT INTO user_rank (id, has_hobbies) values (user_id, CALL
hasUserPassedTest(user_id));
END LOOP;
CLOSE cur1;
END
I am using CALL to invoke my function inside the stored procedure but does not work. How do I call my function within a stored procedure?