我需要运行一个脚本来更新' given_name'列并将所有首字母更改为大写..
我目前有这个代码..没有任何反应..
DROP PROCEDURE IF EXISTS CAPITALIZE;
DELIMITER $$
CREATE PROCEDURE CAPITALIZE()
BEGIN
DECLARE n INT DEFAULT 0;
DECLARE i INT DEFAULT 1;
SELECT COUNT(*) INTO n FROM users;
-- SET n = (SELECT COUNT(*) FROM users);
SET i=0;
WHILE (i<=n) DO
UPDATE users SET given_name = CONCAT( UPPER(SUBSTR(given_name,1,1)), SUBSTR(given_name,2)) WHERE user_id = n;
UPDATE users SET last_name = CONCAT( UPPER(SUBSTR(last_name,1,1)), SUBSTR(last_name,2)) WHERE user_id = n;
SET i = (i + 1);
END WHILE;
END $$
DELIMITER ;
答案 0 :(得分:4)
正如@Drew所提到的,你可能最好只为整个UPDATE
表做一条users
语句:
UPDATE users
SET given_name = CONCAT( UPPER(SUBSTR(given_name,1,1)), LOWER(SUBSTR(given_name,2))),
last_name = CONCAT( UPPER(SUBSTR(last_name,1,1)), LOWER(SUBSTR(last_name,2)))
要关闭安全更新模式,阻止上述查询正常工作,请输入:
SET SQL_SAFE_UPDATES = 0;
这是一个工作小提琴,演示了上述问题: