mysql 使用 phpmyadmin 在存储过程中调用函数
mysql calling a function inside a stored procedure using phpmyadmin
我有以下有效的代码:
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
它使用游标在 table 用户内部循环并将所有用户 ID 复制到 table user_rank。我在我的数据库中定义了一个名为 "hasUserPassedTest" 的函数,它给定了一个用户 ID return 10 或 0。我想在上面的循环中调用该函数并将其插入 user_rank table 但以下代码不起作用:
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
我正在使用 CALL 在存储过程中调用我的函数,但不起作用。如何在存储过程中调用我的函数?
I am using CALL to invoke my function inside the stored procedure but does not work
CALL
不用于执行函数。
The CALL statement invokes a stored procedure that was defined previously with CREATE PROCEDURE.
it loops inside the table users using a cursor and copy all the user ids into the table user_rank.
为什么这么复杂?过程、游标、处理程序、循环...一个简单的查询就足够了:
INSERT INTO user_rank (id, has_hobbies)
SELECT user_id, hasUserPassedTest(user_id)
FROM users;
更新
插入可以与table创建相结合:
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
SELECT user_id AS id, hasUserPassedTest(user_id) AS has_hobbies
FROM users;
一个微妙之处 - 插入的字段将是 table 结构中的最后一个。如果字段顺序有意义,则必须在选择部分提及所有字段:
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
SELECT user_id AS id,
hasUserPassedTest(user_id) AS has_hobbies
0 AS `passed_test`,
0 AS `has_picture`,
0 AS `won_a_job`,
0 AS `is_prolancer`,
0 AS `is_verified`,
0 AS `has_portfolio`,
0 AS `has_likes`,
0 AS `has_disputes`,
0 AS `has-earnings`,
0 AS `has_feebacks`,
0 AS `has_invitations`,
0 AS `has_views`
FROM users;
DROP TABLE
保持单独的查询:(
我有以下有效的代码:
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
它使用游标在 table 用户内部循环并将所有用户 ID 复制到 table user_rank。我在我的数据库中定义了一个名为 "hasUserPassedTest" 的函数,它给定了一个用户 ID return 10 或 0。我想在上面的循环中调用该函数并将其插入 user_rank table 但以下代码不起作用:
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
我正在使用 CALL 在存储过程中调用我的函数,但不起作用。如何在存储过程中调用我的函数?
I am using CALL to invoke my function inside the stored procedure but does not work
CALL
不用于执行函数。
The CALL statement invokes a stored procedure that was defined previously with CREATE PROCEDURE.
it loops inside the table users using a cursor and copy all the user ids into the table user_rank.
为什么这么复杂?过程、游标、处理程序、循环...一个简单的查询就足够了:
INSERT INTO user_rank (id, has_hobbies)
SELECT user_id, hasUserPassedTest(user_id)
FROM users;
更新
插入可以与table创建相结合:
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
SELECT user_id AS id, hasUserPassedTest(user_id) AS has_hobbies
FROM users;
一个微妙之处 - 插入的字段将是 table 结构中的最后一个。如果字段顺序有意义,则必须在选择部分提及所有字段:
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
SELECT user_id AS id,
hasUserPassedTest(user_id) AS has_hobbies
0 AS `passed_test`,
0 AS `has_picture`,
0 AS `won_a_job`,
0 AS `is_prolancer`,
0 AS `is_verified`,
0 AS `has_portfolio`,
0 AS `has_likes`,
0 AS `has_disputes`,
0 AS `has-earnings`,
0 AS `has_feebacks`,
0 AS `has_invitations`,
0 AS `has_views`
FROM users;
DROP TABLE
保持单独的查询:(