简体   繁体   English

如何在mysql的存储过程中更新临时表的行

[英]how to update rows of temporary table in stored procedure in mysql

i wrote a stored procedure(getAllInitializedContact())to get data from diffrent tables.getAllInitializedContact()我写了一个存储过程(getAllInitializedContact())从不同的表中获取数据。getAllInitializedContact()

CREATE DEFINER=`root`@`192.168.1.15` PROCEDURE `getAllInitializedContactNext`()
BEGIN

 DROP TEMPORARY TABLE IF EXISTS temp_users;
 CREATE TEMPORARY TABLE temp_users (
                                SELECT

                                c.id as contactId,
                                c.first_name as contactName,
                                c.email_address as contactEmailAddress,
                                sl.id as subscriberListId,
                                sl.name as subscriberListName, 
                                sl.display_name as subscriberListDisplayName,
                                sl.from_email_address,<br/>
                                sl.opt_in_msg_subject as subject,
                                sl.opt_in_msg_content as content,
                                sl.opt_in_msg_signature as signature,
                                csl.identifier
                                FROM contact c

                                          INNER JOIN contact_subscriber_list csl ON csl.contact_id=c.id
                                          INNER JOIN subscriber_list sl ON sl.id=csl.sub_list_id
                                          INNER JOIN contact_sub_list_status csls ON csls.id=csl.status_id  where csls.description='initialized');
END

but now i want to update that resultset.so i create a temporary table(temp_users) and i need to do some update to some column in temporary table(temp_users).but i cant understand how can i iterate over that and update the temp_table.i tried using while loop and dont know how it apply(while loop).can i do this using while loop?但现在我想更新结果集。所以我创建了一个临时表(temp_users),我需要对临时表(temp_users)中的某些列进行一些更新。但我无法理解如何迭代并更新 temp_table。我尝试使用 while 循环,但不知道它是如何应用的(while 循环)。我可以使用 while 循环来做到这一点吗? and how can i apply it?need help我该如何应用它?需要帮助

regards问候
kosala科萨拉

Not sure why you want to use a cursor to update the rows in your tmp table when you can simply just update them - see following example:当您可以简单地更新它们时,不确定为什么要使用游标来更新 tmp 表中的行 - 请参见以下示例:

drop table if exists foo;
create table foo
(
foo_id int unsigned not null auto_increment primary key,
value int unsigned not null default 0
)
engine=innodb;

insert into foo (value) values (10),(20),(30);

drop procedure if exists bar;

delimiter #

create procedure bar()
begin

create temporary table tmp engine=memory select * from foo;

update tmp set value = value + 100;

select * from tmp;

drop temporary table if exists tmp;

end #

delimiter ;

call bar();

I was making a mistake using the update so it was not working.我在使用更新时犯了一个错误,所以它不起作用。 I though it might help someone so am sharing:我虽然它可能会帮助某人,所以我分享:

For the update to work with a temp table, data to be updated must match the column type you want to update.要使更新与临时表一起使用,要更新的数据必须与您要更新的列类型相匹配。 You can't update a string/text/varchar column with a number.您不能用数字更新字符串/文本/varchar 列。 This would just not work.这根本行不通。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM