我是新手存储程序。我有客户表,它有像customerid,customerName,companyName这样的列。我正在创建新列email_address。哪个工作正常。现在我正在尝试使用商店程序为每个客户添加电子邮件地址值。我在电子邮件地址中输入customername和companyName。但不知怎的,它没有更新。这是变量声明后的代码
CREATE PROCEDURE test()
BEGIN
DECLARE variable
DECLARE CURSOR FOR
SELECT CustomerID, CompanyName, ContactName, EmailAddress
FROM customers;
DECLARE EXIT HANDLER
OPEN cursor_name;
WHILE row_not_found = FALSE
DO
FETCH customers_cursor
INTO customer_id_var, company_name_var, contact_name_var, email_id_var;
SET email_part1 = REPLACE (contact_name_var, ' ', '' );
SET email_part2 = substring_index(company_name_var, ' ', 1);
update customers
SET EmailAddress = CONCAT(email_part1, '@', email_part2, '.com')
WHERE CustomerID = customer_id_var;
SET update_count = update_count + 1;
END WHILE;
CLOSE cursor_name;
SELECT * FROM customers;
END//
DELIMITER ;
CALL test();
答案 0 :(得分:1)
你不需要光标:
update customers
set EmailAddress = CONCAT(REPLACE(contact_name_var, ' ', '' ), '@',
substring_index(company_name_var, ' ', 1), '.com');
这比使用游标简单得多,它应该表现得更好。
(我不会评论创建电子邮件地址的实际逻辑,只是说在许多情况下它似乎很可能失败。)