MySQL stored procedures

Source: Internet
Author: User

-stored procedure name and parameters, in parameter in for incoming parameters, out for outgoing parameters, inout for incoming outgoing parametersCreate procedureP_procedurecode (inchSumdatevarchar(Ten))   begin     DeclareV_sqlvarchar( -);--SQL statements that need to be executed     DeclareSymvarchar(6); DeclareVar1varchar( -); DeclareVar2varchar( -); DeclareVar3integer; --define cursor traversal as a marker to determine if all records are traversed     DeclareNo_more_departmentsinteger DEFAULT 0; --define the cursor name C_result     DECLAREC_resultCURSOR  for             SELECTBarcode,barname,barnum fromtmp_table; --declares that a flag variable is set to a value when the cursor has traversed all records     DECLARE CONTINUEHANDLER for  notFOUNDSETNo_more_departments=1; SetSym=substring(Sumdate,1,6);--intercepts the string and assigns it to a traversal     --The connection string forms the complete SQL statement, and the dynamic SQL execution result Recordset is not available in MySQL, so you need to change your mind to place it in a temporary table (note the notation in the code). The general wording is as follows:     --' Create temporary table name (query statement for select);     SetV_sql=Concat'Create Temporary Table tmp_table (select AA as AACODE,BB as Aaname,count (cc) as Ccnum from H', Sym,'where substring (dd,1,8) =" ", Sumdate," "GROUP by AA,BB)'); Set @v_sql=V_sql;--Note It is important to assign a string to a variable (you can not define it before, but start with @)     Preparestmt from @v_sql;--preprocessing requires the execution of dynamic SQL, where stmt is a variable     EXECUTEstmt--Execute SQL statement     deallocate Preparestmt--Release the preprocessing segment     OPENC_result;--open a previously defined cursorREPEAT--keywords for loop statements           FETCHC_result intoVAR1, VAR2, VAR3;--take each record and assign it to the relevant variable, note the order          --executes a query statement and pays the obtained value to a variable @oldaacode (note that a variable starting with @ can be declared without a Declare statement)           Select @oldaacode:=Vcaacode fromT_sumwhereVcaacode=Var1 andDtdate=sumdate; if @oldaacode=Var1 Then    --Judging              UpdateT_sumSetINum=Var3whereVcaacode=Var1 andDtdate=sumdate; Else               Insert  intoT_sum (Vcaacode,vcaaname,inum,dtdate)Values(var1,var2,var3,sumdate); End if; UNTIL no_more_departmentsENDREPEAT;--Loop statement End     CLOSEC_result;--Close Cursors     DROP Temporary TABLEtmp_table;--Delete temporary tableEnd;

MySQL stored procedures

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.