Database---Create functions, store functions, trigger instances

Source: Internet
Author: User

1. Create a function

function float float  floatbegin    # This function is in the body range, so-called "programming Environment"    # "define" session variables and assign values    return @result; End;
function float float  floatbegin    # This function is in the body range, so-called "programming Environment"    #  This range can be used with ordinary variables, already various flow control structures    declarefloatdefault 0;    # define common variables and set initial values    Set result = X*x + y*y;     return result; End;

2, the creation process:

This stored procedure is intended for passing two data and inserting the two data into table enum_test.

CREATE PROCEDURE  pro1 (n int, Xuanxiang varchar)begin    INSERT INTO enum_test (ID, Xuanxiang) VALUES ( N, Xuanxiang); End;

3. Stored procedure with out function

float float float ) begin    = Getpingfanghe (z1, Z2); # Call the function to calculate the sum of squares    POW (@pingfanghe, 0.5); End ; // when the stored procedure is called, the 3rd argument must be a variable

4. Using the Select stored procedure

CREATE PROCEDURE  gettables () begin    * from enum_test;     * from set_test;     * from tab5; End

5, define a trigger, so that the trigger can be inserted in the TAB5 data,
Automatically inserts a piece of data into another table for use by a "foreign partner".
①TAB5 fields: ID, F1, F2, F4, Id2;
② field of another table to be inserted: ID, F1, f2

Create trigger Getsubdata after insert on TAB5 for each  row begin    #set @id = new.id;    #new在触发器中是特定关键字,                        #代表 "data row just inserted", which represents the ID value of the row data    #set @v1 = new.f1;    #新插入数据的f1字段的值    #set @v2 = new.f2;    #新插入数据的f1字段的值    INSERT INTO tab5_sub (F1, F2) values (NEW.F1,NEW.F2); end;

Database---Create functions, store functions, trigger instances

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.