CREATE PROCEDURE Sys_message_user_for_busbase () Begin-Declares a flag done that is used to determine whether the cursor is traversing the completion of the DECLARE do INT DEFAULT 0; --Declare a variable to hold the data extracted from the cursor--especially note that the name cannot be the same as the column used in the cursor, otherwise the resulting data is null DECLARE tid varchar () DEFAULT null; DECLARE tname varchar () DEFAULT NULL; DECLARE thead varchar (255) DEFAULT NULL; DECLARE tmobile varchar (255) DEFAULT NULL; --Declares the SQL statement corresponding to the cursor DECLARE cur cursor FOR select Id,shop_name, Mobile_phone,shop_logo from Hx_busbase; --At the end of the cursor loop, the done is set to 1 DECLARE CONTINUE HANDLER for not FOUND set do = 1; --Execute Query open cur; --iterate through each row of the cursor REPEAT--store a row of information in the corresponding variable FETCH cur into tid,tname, thead,tmobile; If not do then--here you can use Tname, tpass the corresponding information if (select COUNT (1) from hx_message_user where uid=tid) = 0 Theninsert in To Hx_message_user (Id,head,mobile,username,uid,remark) VALUES (Tid,thead,tmobile,tname,tid, "busbase"); End if; #select Tid,tname, Tpass, (select COUNT (1) from btable where Id=tid) as count; End If; UNTIL done END REPEAT; CLOSE Cur;end
#执行
Call Sys_message_user_for_busbase ();
MySQL stored procedure copy a table data to table B