Using Navicat to write MySQL cursors

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.