An example of a stored procedure in MySql

Source: Internet
Author: User

Example of a MySql stored procedure: SQL code www.2cto.com CREATE DEFINER = 'root' @ '% 'Procedure 'proc _ calculate_combo_price' (IN 'comboid' VARCHAR (36), OUT 'price' DOUBLE, OUT 'reallyprice' DOUBLE) language SQL not deterministic contains SQL SECURITY DEFINER comment' calculation service package price 'in in declare minPeriodType varchar (36); declare minPeriodNum INT; declare fetchOk INT; declare distinct INT; declare discountRate DOUBLE; declare discountPrice DOUBLE; declare billUnitsId varchar (36); declare serviceDetailId varchar (36); declare value INT; declare unitsPrice INT; declare unitsPeriod varchar (36); declare unitsPeriodNum INT; declare optionsFlag INT; declare minValue INT; declare stepLength INT; declare setpPrice DOUBLE; declare combo cursor for select combo. min_period_type_id, combo. min_period_num, combo. discount_rate, combo. discount_price from srv_service_combo combo where combo. id = comboId; declare comboDetail cursor for select detail. options_value, detail. value, detail. service_detail_id from srv_service_combo_detail detail where detail. delete_flag = 0; declare continue handler for not found set fetchOk = 1; set fetchOk = 0; open combo; combo_loop: loop FETCH combo INTO minPeriodType, minPeriodNum, discountRate, discountPrice; IF fetchOk = 1 then leave combo_loop; end if; end LOOP combo_loop; close combo; select period. days_number into comboPeriodNum from srv_period_type period where period. id = minPeriodType; set fetchOk = 0; set price = 0; open comboDetail; detail_loop: loop FETCH comboDetail INTO billUnitsId, value, serviceDetailId; IF fetchOk = 1 then leave detail_loop; ELSE select units. price into unitsPrice from srv_bill_units units where units. id = billUnitsId; select units. period_type_id into unitsPeriod from srv_bill_units units where units. id = billUnitsId; select period. days_number into unitsPeriodNum from srv_period_type period where period. id = unitsPeriod; select sd. options_flag into optionsFlag from srv_service_detail sd where sd. id = serviceDetailId; IF optionsFlag = 1 then set price = price + unitsPrice * (comboPeriodNum/unitsPeriodNum); ELSE select sd. min_value into minValue from srv_service_detail sd where sd. id = serviceDetailId; select sd. step_length into stepLength from srv_service_detail sd where sd. id = serviceDetailId; select sd. step_price into setpPrice from srv_service_detail sd where sd. id = serviceDetailId; set price = price + (unitsPrice + (value-minValue)/stepLength * setpPrice) * (comboPeriodNum/unitsPeriodNum); end if; end if; end LOOP detail_loop; close comboDetail; set reallyPrice = price * discountRate-discountPrice; END

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.