A: plsql total summarized as
. 1 .pl / SQL basic syntax structure
DECLARE, the begin , Exception , End,: =, =, and other key character 2 recording type ... type. IS Record (,,); (corresponding to the member variable java) 3 flow control 3.1 condition determining (two kinds) Method 1: IF ... the then ... ... ELSIF the then ... the else ... End IF ; Second way: Case ... When ... the then .. . End ; 3.2 loop structure (three kinds) Method 1: loop ... Exit When ...End Loop; Second way: the while ... End Loop; Three ways: for I in ... ... Loop End Loop; 3.3 keyword goto out of the loop corresponds to the Java ( BREAK ) Exit Exit 4 cursor used (similar. iterator in the java iterators for traversing a set of) 5 exception handling (three ways). 6 storage. 6 .1 stored function: return value 6 .2 stored procedure: no return value 7 flip-flop 8 index 9 . view
Two: declare variables with
1 specify the type declaration, and a length (same type and table type, length to be greater than or equal table field length). V_sal Number ( 10 , 2 ); v_email VARCHAR2 ( 20 is ); v_hire_date DATE; 2 . Directly field in a table the property assigned to the variable (recommended), less error-prone v_sal employees.salary % type; v_email employees.email % type; v_hire_date employees.hire_date % type;
III. Record Type Definition Record (java in the subject)
1 . Declared a class member behind the note record type with a comma, the last one without comma semicolon in parentheses following type emp_record IS Record ( v_sal employees.salary % type, v_email employees.email % type, v_hire_date employees.hire_date % type ) ; - the definition of a record type of the member variable v_emp_record emp_record; - the back can be used directly v_emp_record variable name called. examples: dbms_output.put_line ( ' wage ' || v_emp_record.v_sal || ' , the message ' || v_emp_record.v_email | | ' , the date ' || v_emp_record.v_hire_date); 2 . Directly create a record type table % ROWTYPE (recommended) in the table all the fields and their types are declared to record this type, in order to call back v_emp_record the Employees % ROWTYPE; examples: DBMS_OUTPUT.PUT_LINE ( ' salary ' || v_emp_record.salary || ' , the message ' || v_emp_record.email || ' , date ' || v_emp_record.hire_date);
Four: Assignment Problem: = and =
DECLARE v_emp_id Number (10); the begin v_emp_id : = 123; Update the Employees SET the salary = the salary + 100 WHERE the employee_id = v_emp_id; DBMS_OUTPUT.PUT_LINE ( 'executed successfully'); End; - with a variable assignment: = --sql inside Assignment a = --plsq with equal function determines =
Five: conditional statement (two kinds) (recommended if..else) can handle a variety of conditions to determine, case not
1.if ... then ... elsif ... then ... else ... end if;
- Requirements: 150 Query the number of employees wages, salaries if it is greater than or equal to 10,000 print 'the salary> = 10000'; - if between 5,000 to 10,000, the print '5000 <= salary <10000' ; otherwise, print 'the salary <5000' DECLARE v_sal employees.salary% type; - record table structure type v_temp VARCHAR2 ( 50 ); the begin SELECT the salary INTO v_sal from the Employees WHERE the employee_id = 150 ; IF v_sal > = 10000 the then v_temp: = ' the salary> 10000 = ' ; ELSIF v_sal > = 5000 the thenv_temp: = ' 5000 <= the salary <10000 ' ; the else v_temp: = ' the salary <5000 ' ; End IF ; DBMS_OUTPUT.PUT_LINE ( ' real wage: ' || v_sal || ' , the output information: ' || v_temp) ; End ;
2.case ... when ... then ... end;
(1) number of digital case
- Requirements: 150 Query the number of employees wages, salaries if it is greater than or equal to 10,000 print 'the salary> = 10000'; - if between 5,000 to 10,000, the print '5000 <= salary <10000' ; otherwise, print 'the salary <5000' DECLARE v_sal employees.salary % type; v_temp VARCHAR2 ( 50 ); the begin SELECT the salary INTO v_sal from the Employees WHERE the employee_id = 150 ; v_temp: = Case the trunc (v_sal / 5000 ) When 0 the then ' the salary> = 10000 ' When . 1 the then ' 5000 <= the salary <10000 ' the else ' the salary <5000 ' End ; DBMS_OUTPUT.PUT_LINE ( ' real wage: ' || v_sal || ' , the output information: ' || v_temp); End ;
(2) varchar2 string case
- Requirements: 122 check out JOB_ID employees, if a value of 'IT_PROG', print 'the GRADE: A'; - 'AC_MGT', print 'GRADE B', 'AC_ACCOUNT' , print 'GRADE C'; otherwise the print 'the GRADE D' DECLARE v_job_id employees.job_id % type; v_temp VARCHAR2 ( 20 is ); the begin SELECT the job_id INTO v_job_id from the Employees WHERE the employee_id = 122 ; v_temp: = Case v_job_id When ' IT_PROG ' the then ' the GRADE: A ' When 'AC_MGT' then 'GRADE B' when 'AC_ACCOUNT' then 'GRADE C' else 'GRADE D' end; DBMS_OUTPUT.put_line('job_id:'||v_job_id||' 打印信息: '||v_temp); end;
Six: loop structure (three kinds)