MySQL custom stored procedures and triggers

Source: Internet
Author: User

MySQL custom stored procedures and triggers--stored Procedure demonstration drop PROCEDURE IF EXISTS pro_test; CREATE PROCEDURE pro_test (in num_in int,out num_out int,inout num_inout) begin//do anything you wantend; parameter interpretation: In: Is the input parameter, input Parameters are passed into the stored procedure as parameters, changing its value will not change its original value, equivalent to the value passed out: This is the output parameter, in the stored procedure to give its value, even if the previous value, its value is only null, The equivalent is to create a new value in the stored procedure and pay out parameters and output INOUT: This is an input and output parameter, it can be understood as the sum of the first two, the simplest is to understand how to pass the object in the command line to call: SET @NM_IN =1, @NM_OUT =2, @NM_INOUT = 3;call proc_test (@NM_IN, @NM_OUT, @NM_INOUT); How to call in Java: Find a way to get connection, getsession () in Hibernate. Connection (); Use Java.Sql.CallableStatemet call=connection (). Preparestatement ("Call proc_test"); Call.executequery () The ResultSet object will be returned, and the number of rows affected will be returned by Call.executeupdate (). --Trigger sample trigger According to the trigger type can be divided into: Insert,update,delete Trigger, according to the trigger time can be divided into: before,after, according to the content of the new and old can be divided into: old,new, where the deletion of only the original, new operation only New, Modify action both have eg: without modifying the USER table once, insert the data before and after the modification to the User_back table: CREATE TRIGGER tri_upgrade_user before UPDATE on USER for each Rowbegininsert into User_back (username,userpwd) VALUES Username,old. USERPWD); INSERT into User_back (username,userpwd) VALUES (NEW. Username,new.USERPWD); END; it's done.

Related Article

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.