PostgreSQL Foundation Finishing (II.)

Source: Internet
Author: User
Tags postgresql

Stored Procedures

Implementation function: For salary scale under 30 years of age, salary increase 10% 30 to 40 increase 20% 40 + 30% + bonus (admission) return average compensation

    • To create a table:
DROP TABLEEmps;CREATE TABLEEmps (useridint PRIMARY KEY, ageint, salary numeric);INSERT  intoEmpsVALUES(Ten, -,1000.0),( One, -,1500.0),( A, -,1300.0),( -, *,3000.0), ( -, $,4000.0);
    • To create a stored procedure:
    DROP FUNCTION IF EXISTSadd_salary (_bonus numeric); CREATE OR REPLACE FUNCTIONadd_salary (_bonus numeric)RETURNSNumeric as$body $DECLARELevel1 Numeric:=  -; Level2 Numeric:= +; Res Numeric:= 0; Pageint; Puseridint;                     MyCursor Refcursor; BEGIN            OPENMyCursor for SELECTUserID, age fromEmps; FETCHMyCursor intoPuserid, page; RAISE NOTICE'Age is :----%', Puserid||','||page;  whileFOUND LOOPIFPage<=Level1 Then                    UPDATEEmpsSETSalary=Salary* 1.1 WHEREUserid=Puserid; elsif page>Level1 andPage<=Level2 Then                    UPDATEEmpsSETSalary=Salary*1.2 WHEREUserid=Puserid; ELSE                    UPDATEEmpsSETSalary=Salary*1.3 WHEREUserid=Puserid; END IF; FETCHMyCursor intoPuserid, page; ENDLOOP; SELECT AVG(SALARY) fromEmps intoRes; RETURNRes; END$body $ Language plpgsql;
    • Execution results

PostgreSQL Foundation Finishing (II.)

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.