There is nothing less than a simple and straightforward example:
CREATE PROCEDUREpro_users ()begin DECLAREmyIDint; DECLARENoint; DECLAREMyselleridChar(Ten); DECLAREMyLinkChar( -); DECLAREMyipChar( -); DECLAREMyCursorCURSOR for SelectId fromTable1; #把select出来的数据全部保存到游标mycursor中DECLARE CONTINUEHANDLER for notFOUNDSETNo=1; #当读到数据的最后一条时, set the no variable to 1SETNo=0; #初始化变量no为0OPENmycursor; #打开游标 whileNo=0Do #判断是不是到了最后一条数据FetchMyCursor intomyID, #读取游标中的数据一一复给变量myID. SelectSellerid,link,ip intoMysellerid,mylink,myip fromTable2Order by RAND() LIMIT1; UpdateTable1SetWangba_uid=Ceil (Rand()* -), Wangba_sellerid=Mysellerid, Wangba_link=MyLink, Wangba_ip=MyipwhereId=myID; End while; Closemycursor; #最后关闭游标. There's so much data in the cursor that you always have to clean out. End
Note: These declarative syntax must be written in the stored procedure to take effect, otherwise navicat will prompt for syntax errors.
Finished:
Call Pro_users ()
Unlike SQL, MySQL executes stored procedures using the call command.
To delete a stored procedure:
Drop PROCEDURE pro_users
Delete stored procedures, MySQL and SQL all use drop.
Other:
Select into from Order by RAND 1;
MySQL assigns values to variables using select into,RAND () random record, LIMIT 1 instead of top 1
This sentence in the SQL inside the wording:
Select Top 1 Mysellerid=Sellerid,mylink=Link,myip=fromorder by newid ();
Using Navicat to write MySQL cursors