涉及到表的处理请参看原表结构与数据 Oracle建表插数据等等
建包
-- 建立包头 create package mypkg is procedure set_ctx(p_name in varchar2, p_value in integer); --不需要初始化 --procedure init; end; /
建立包体
create or replace package body mypkg is procedure set_ctx(p_name in varchar2, p_value in integer) as begin --'myctx' 是全局上下文名称 --与建立上下文中的myctx一致 dbms_session.set_context('myctx', p_name, p_value, NULL, NULL); end; begin null; end; /
设置值
call mypkg.set_ctx('var1', 1234); call mypkg.set_ctx('var2', 2234);
查询
select sys_context( 'myctx', 'var1' ) var1, sys_context( 'myctx', 'var2' ) var2 from dual;
--创建包。声明该包有一个过程update_sal,声明该包有一个函数annual_income:
create package fj_package is procedure update_sal(name varchar2,newsal number); function annual_income(name varchar2) return number; end; /
--给包fj_package实现包体 ,来修改某个雇员的工资或者查询某个雇员的工资
create or replace package BODY fj_package is procedure update_sal(name varchar2, newsal number) is begin update tb_Employee set sal = newsal where ename = name; end; function annual_income(name varchar2) return number is annual_salary number; begin select sal * 12 + nvl(comm, 0) into annual_salary from tb_Employee where ename = name; return annual_salary; end; end; /
call fj_package.update_sal('SCOTT', 1400);--调用