PL/SQL programming study notes

1, be understood that PL / SQL programming concepts;
2, master PL / SQL programming techniques; stored procedures, functions, triggers, including
an understanding of PL / SQL programming concepts;
1, Procedural Language / SQL variables can be defined using embedded SQL constants ;
2, procedures, functions, trigger the oracle;
. 3, PL / SQL is a powerful procedural language;
4, stored procedures and functions may be invoked in java;
advantages: to improve the operating performance of the application; modular Design (paging process, order process, transfer process


); reduce network transmission; improve security;
disadvantages: poor portability, oracle stored procedures cannot be used in DB2;
development tools: 1. SQLPLUS; PL/SQL developer


example Program:
create table pro1(proid varchar2(10), passwd varchar2(20));
alter table pro1 add (proname varchar2(20));
create table for backup;
create procedure sq_pre1 is
begin 
insert into pro1 values('001',' fanjf');
end;


oracle command line view error;
show error;
If the procedure is called:
1. The exec procedure name (parameter 1, parameter 2...)
2. The call procedure name (parameter 1, parameter 2...)
 Developer requirements: 1. SQL statement; 2. Syntax;


block programming classification : Stored procedure; function; trigger; package;


writing specification:
1. Comment: single-line
comment--; multi-line comment/*-----*/ 2. Identifier: variable v_; constant c_; cursor_cursor; Exception: e_


block structure: definition part, execution part, exception handling part;
declear--define constants, variables, cursor exceptions, complex data types;
begin--execution part;
exception--exception handling part
end;
demo1
set serveroutput on; --Output options open
begin
dbms_output.put_line('hello');
end;


demo2: a block with defined variables


declare
   v_ename varchar2(20);
   v_id varchar2(20);
begin 
 select passwd,proid into v_ename,v_id from pro1 where proid =&aa;
 DBMS_OUTPUT.PUT_LINE ( 'ID:' v_ename || || '-' || v_id);
Exception
When the then NO_DATA_FOUND
DBMS_OUTPUT.PUT_LINE ( 'i for data not found ah ah ah');
End;





memory procedures, functions, triggers, packages
stored procedure: You can input parameters, output parameters can be used to create procedure command to establish;


Demo:
1, write a stored procedure, enter the employee, the new wage, you can modify wages
2, how to call a stored procedure ;
3, how to call stored procedures in Java;
4, the Java stored procedure return value;
- creation of
the create procedure sp_pro3 (SPID VARCHAR2, SPName VARCHAR2) IS
the begin
- execution, according to the numbering change the username
update pro1 set proname = spname where proid=spid;
end;
/
--PL/SQL call procedure
exec sp_pro3('001','haha');
--call stored procedure in JAVA
String strCall = "{ call sq_pre2 }";//Note that there must be spaces. Call
conn = getConnection();
callLableStmt = conn.prepareCall(strSql);
bRst = callLableStmt.execute();


Stored procedure call with parameters:
strCall = "{ call sq_pre3(?,?) }";
iRst = executeCall(strCall,new Object[] {"001","fan"});


conn = getConnection();callLableStmt = conn.prepareCall(strSql);
for (int i = 0; i <params.length; i++) {
callLableStmt.setObject(i + 1, params[i]);
}
bRst = callLableStmt.execute();


Function:
function must have renturn
create or replace function sp_fun2 (spName varchar2) return 
number is yearSal number(10,2);
begin
select nvl(sal,99999)*12 into yearSal from pro1 where proname=spName;
return yearSal;
end;
/


Call in SQL/PLUS:
SQL> var income number
SQL> call sp_fun2('fanjf1') into:income;


Method called
income
---------
119.88 Call in
JAVA: like using SQL query statements:
select sp_fun2('fanjf1') from dual;


package:
write paging stored procedures;
logically combine procedures and functions. Sum by package specification The package body consists of two parts; the
create package command creates a package;
example:
create or replace package sp_package is
     procedure update_sal(pname varchar2, newsal number);
     function annual_income(pname varchar2) return number;
end;
/The
package specification only contains the declaration of procedures and functions; it is not implemented; the
package body can use the create package body command; to
implement the package body:
create or replace package body sp_package is
procedure update_sal(pname varchar2, newsal number)--note that the parameter name must be to


the same
IS
the begin 
    Update Pro1 SET SAL = newsal WHERE proname = pname;
End;
     function annual_income (pname VARCHAR2) return Number
IS annual_sal Number;
the begin
SELECT NVL (SAL, 2) * 12 is INTO annual_sal from Pro1 WHERE proname = pname;
return annual_sal ;
end;
end;
/


The process and function of calling the package need to add the package name;
exec sp_package.update_sal('fanjf1',120);


trigger:
Nature is implicit execution of stored procedures;
when the trigger is defined, must be defined trigger events and trigger actions, common triggering events include


insert, update, delete create trigger can be used


to define and use variables
four types of variables:
scalar (scalar), Composite (composite), reference (reference), lob (large object)
scalar: used to store ordinary variables. Variable-length character string v_ename varchar2(10); decimal v_sal 


number(6,2); define decimal with default value v_sal number(6,2):=5.4; define date type:


v_hiredate date; define a boolean variable, it cannot be empty, The initial value is false v_valid boolean 


not null default false;
Variable assignment: =
Example: input employee number, display name, salary, personal income tax (tax rate 0.03)
declare
c_tax_rate number(3,2):=0.03;--tax rate
v_ename pro1. proname%type;--name or v_ename varchar2(10);
v_sal number(10,2);
v_tax_sal number(10,2);
begin
proname SELECT, SAL INTO v_ename, v_sal from Pro1 proid = & WHERE NO;
- calculating tax
v_tax_sal: * = v_sal c_tax_rate;
- output
dbms_output.put_line ( 'Name:' || v_ename || 'salary' || v_sal || 'Income tax'||


v_tax_sal);
end;
/

Guess you like

Origin blog.csdn.net/jifeijixufly/article/details/17273353