1.无变量匿名快
begin dbms_output.put_line(‘Hello World‘); end;
2.有变量的匿名块,定义变量:
declare v_ename varchar2(10); v_sal number(7,2); begin select ename,sal into v_ename,v_sal from emp where empno=&n; dbms_output.put_line(‘Employee name is: ‘||v_ename); dbms_output.put_line(‘Employee sal is: ‘||v_sal); end;
3.定义常量和变量并赋值:
declare v_ename VARCHAR2(10); v_sal NUMBER(7,2); c_empno constant NUMBER(4):=7788; //声明一个常量 begin select ename,sal into v_ename,v_sal from emp where empno=c_empno; dbms_output.put_line(‘Employees name is: ‘||v_ename); dbms_output.put_line(‘Employees salary is: ‘||v_sal); end;
4.在执行部分给变量赋值:
declare v_ename varchar2(10); v_sal number(7,2); v_empno number(4); begin v_empno :=7788; select ename,sal into v_ename,v_sal from emp where empno=v_empno; dbms_output.put_line(‘Employee name is: ‘||v_ename); dbms_output.put_line(‘Employee sal is: ‘||v_sal); end;
5.异常处理:
declare v_ename varchar2(10); v_sal number(7,2); begin select ename,sal into v_ename,v_sal from emp where empno=&n; dbms_output.put_line(‘Employee name is: ‘||v_ename); dbms_output.put_line(‘Employee sal is: ‘||v_sal); exception when no_data_found then dbms_output.put_line(‘Please input current number!‘); end;
6.通过%type,保证变量的数据类型和表中字段的数据类型一致:
declare v_ename emp.ename%type; v_sal emp.sal%type; begin select ename,sal into v_ename,v_sal from emp where empno=&n; dbms_output.put_line(‘Employee name is: ‘||v_ename); dbms_output.put_line(‘Employee sal is: ‘||v_sal); exception when no_data_found then dbms_output.put_line(‘Please input current number!‘); end;
时间: 2024-10-13 02:35:10