Oracle defined variables, assignment and use

First, when do cmd in the scott password prompt an error, so you can change it, scott unlock command is:

 Users log on to the system:
cmd
sqlplus system / tigertiger
the ALTER scott IDENTIFIED by the User "Tiger" the Account UNLOCK;

- block structure learning
- Learning define the variable (data type Learn) and assignment
- Learn: Type various PL / SQL block

- structural blocks
- Case: According to the job number, employee name output
 SELECT * FROM emp;

--Piece!

DECLARE
  v_empno NUMBER(4);
  v_ename VARCHAR2(10);
BEGIN
   v_empno: = & Please enter the number of workers;
   - To query results into variables must
   SELECT ename INTO v_ename FROM emp WHERE empno=v_empno;
   dbms_output.put_line(v_ename);
END;

 

--abnormal

DECLARE
  v_empno NUMBER(4);
  v_ename VARCHAR2(10);
BEGIN
   v_empno: = & Please enter the number of workers;
   - To query results into variables must
   SELECT ename INTO v_ename FROM emp WHERE empno=v_empno;
   dbms_output.put_line(v_ename);
  
   EXCEPTION
      WHEN OTHERS THEN
        dbms_output.put_line ( 'implementation of mistake, boss!');
END;

  

- Summary of
the DECLARE
   - variable region (not defined in the begin..end)
the BEGIN
    - service code area
    excetipn
    - capture zone exception
END;  


- definition of variables (data type Learn)
--2 species: saving a simple variable = value; storing a plurality of composite variable values =

- simple variable type: char, varchar2, number, date ( and also column type), boolean, type Field%.
- composite variable: Table% rowtype, record

DECLARE
  v_empno NUMBER(4);
  v_ename VARCHAR2(10);
  v_job emp.job% TYPE; - If the field type changes, the script does not need to change
BEGIN
   v_empno: = 7566;
   SELECT ename,job INTO v_ename,v_job FROM emp WHERE empno=v_empno;
   dbms_output.put_line(v_ename);
   IF(v_job='MANAGER')THEN
      dbms_output.put_line ( 'manager, did not eat rice.');
   END IF;
END;

  


- need above, the number of variables into a record object

DECLARE
  v_empno NUMBER(4);
  my_row emp%ROWTYPE;
BEGIN
   v_empno: = 7566;
   SELECT * INTO my_row FROM emp WHERE empno=v_empno;
   dbms_output.put_line(my_row.ename||','||my_row.sal);
END;

  

- sometimes an entire column is not required, only a few columns. This time with record

DECLARE
    - Custom complex type my_emp
   TYPE my_emp_type IS RECORD(
      yg_name emp.ename%TYPE,
      yg_sal emp.sal%TYPE
   );
   my_row my_emp_type; - Variable name Data type
BEGIN
   SELECT ename,sal INTO my_row FROM emp WHERE empno=7788;
   dbms_output.put_line(my_row.yg_name||','||my_row.yg_sal);
END;

  

- Extended: Oracle no boolean type?
- Reflections: Gender true = male, false = female. Shortcoming? Sex = confidential. Therefore, Oracle create alternate with char (1), 1 = 2 = M 3 = M 4 = Simon Confidential

--Oracle various collection type, the corresponding array of java.
- reference variables: explain again in the future to explain the stored procedure.

- bind variables (execution command window, the bound variables :)
the SQL> var name VARCHAR2 (10)
the SQL> Execute: name: = 'Hello';


PL/SQL procedure successfully completed

SQL> print name;

name
---------
hello

 

Figure: 

declare
   l_dept    integer := 20;
   currtime  date := sysdate;
   l_nam     varchar2(20) := to_char(add_months(trunc(sysdate),-12),'yyyymmdd');  -- to_char(sysdate,'MM')-13;

   type num_list is varray(5) of number;
   arr_id num_list := num_list(100,101,123,33,234);

 begin
      l_dept := 30;
      dbms_output.put_line(l_dept);
      dbms_output.put_line(currtime);
      dbms_output.put_line(l_nam);
      dbms_output.put_line(arr_id(1));      
 end;

Variable definition:

l_dept definition is an integer,

currtime to a date,

l_nam for the character back and forth for 12 months of the date of the assignment, and - to_char (sysdate, 'MM') - 13; there will be negative.

type num_list is varray (4) of number; data type is defined as an array of integers, and the array length is 4,

arr_id num_list: = num_list (100,101,123,33,234); using an array type definition arr_id num_list variable definition and assignment of arr_id.

dbms_output.put_line function is output in DMS Output tab, note: dbms_output.put_line must be placed within the begin and end .

truncate table t4; - clear data in table
declare
op nvarchar2(100);
i int ;
j int :=100;
begin
  while j<200 loop
  select nvl(max(id),0) +1 into i from t4;
  insert into t4 values(i,j,'test'||i);
  dbms_output.put_line(i);
  j: = j + 1;
  end loop;
end;
-- select * from t4;

  

  

 

Guess you like

Origin www.cnblogs.com/lgx5/p/11290773.html