oracle如何建包,oracle怎么创建包

oracle中利用存储过程、函数、包

请说明您所谓的排名是怎么排名的 是按工号还是按工资排名???

成都创新互联公司公司2013年成立,先为融水等服务建站,融水等地企业,进行企业商务咨询服务。为融水企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

请问Oracle中如何在通过创建一个package,然后在package中创建procedure的语法是怎么样的啊

先create package,在里面声明procedure

然后再create package body,在package body里面编写procedure的代码即可(相当于create procedure)。

包里没有create procedure

示例如下:

CREATE OR REPLACE PACKAGE pkg_test IS

PROCEDURE pro_test(V_OLD IN VARCHAR2,V_NEW IN VARCHAR2);

...

END test;

/

CREATE OR REPLACE PACKAGE BODY pkg_test IS

...

PROCEDURE pro_test(V_OLD IN VARCHAR2,V_NEW IN VARCHAR2) IS

BEGIN

...

END;

...

END pkg_test;

oracle11g部署搭建

1.配置hostname

[root@oracledb ~]#hostnamectl set-hostname oracledb

[root@oracledb ~]#echo " 10.100.1.10 oracledb" /etc/hosts

2.关闭selinux和防火墙

[root@oracledb ~]#sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config

[root@oracledb ~]#setenforce 0

[root@oracledb ~]#systemctl stop firewalld

[root@oracledb ~]#systemctl disable firewalld

[root@oracledb ~]#systemctl status firewalld

3.下载Oracle软件包 且上传到服务器上,进行解压缩

建议使用winSCP工具进行上传到服务器的/opt目录下

[root@oracledb ~]# ls -lh /opt/

-rw-r--r-- 1 root root 1.2G Mar 30 16:12 linux.x64_11gR2_database_1of2.zip

-rw-r--r-- 1 root root 1.1G Mar 30 16:11 linux.x64_11gR2_database_2of2.zip

4.安装oracle环境所需要的依赖包-配置YUM源

安装依赖包之前,一定要设置好服务器上的安装源,原因是:可以在没有互联网的时候安装服务所需依赖包;常用的安装源有两种方式:

(1) 配置本地yum源

新建配置文件vi /etc/yum.repos.d/localsource.repo输入如下内容:

[localsource]

name=localsource

baseurl=

enabled=1

gpgcheck=0

(2) 生成本地yum 缓存

注意:从Oracle 11g 11.2.0.2版本开始,在Linux x86_64上安装时,只需要安装64位的软件包,但是,对于11.2.0.2之前的任何Oracle database 11g,下表中列出的32位和64位的安装包都必须安装

5.安装oracle环境所需要的依赖包-安装依赖包

(1)安装依赖包

[root@oracledb ~]# yum -y install binutils compat-libcap1 compat-libstdc++-33 gcc gcc-c++ glibc glibc-devel ksh libaio libaio-devel libgcc libstdc++ libstdc++-devel libXi libXtst make sysstat unixODBC unixODBC-devel zlib zlib-devel pcre-devel unzip

(2)安装完成后,检查依赖是否全部安装成功

[root@oracledb ~]# rpm -q binutils compat-libcap1 compat-libstdc++-33 gcc gcc-c++ glibc glibc-devel ksh libaio libaio-devel libgcc libstdc++ libstdc++-devel libXi libXtst make sysstat unixODBC unixODBC-devel zlib zlib-devel pcre-devel unzip | grep "not installed"

(3)创建所需的用户组和用户

如果要安装Oracle数据库,通常需要以下本地操作系统用户组和用户:

Oracle inventory组(通常为 oinstall)

OSDBA组 (通常为 dba)

OSOPER组 (通常为 oper)

Oracle软件所有者(通常为 oracle)

创建用户组和用户:

[root@oracledb ~]# groupadd oinstall

[root@oracledb ~]# groupadd dba

[root@oracledb ~]# groupadd oper

[root@oracledb ~]# useradd -g oinstall -G dba oracle

(4)修改oracle用户密码:建议密码配置为字符+数字+特殊字符

[root@oracledb ~]# passwd oracle

6.配置内核参数和资源限制

(1)编辑配置文件vi /etc/sysctl.conf添加如下参数:

vm.max_map_count = 655360

fs.aio-max-nr = 1048576

fs.file-max = 6815744

kernel.shmall = 2097152

kernel.shmmax = 2147483648 ###最小值: 536870912

kernel.shmmni = 4096

kernel.sem = 250 32000 100 128

net.ipv4.ip_local_port_range = 9000 65500

net.core.rmem_default = 262144

net.core.rmem_max = 4194304

net.core.wmem_default = 262144

net.core.wmem_max = 1048576

注意:如果系统中某个参数高于上面的参数的值 ,保留较大的值,上面的数值只是官方要求的最小值,可以根据系统调整数值,以优化系统性能

(2)使内核参数生效

[root@oracledb ~]# sysctl -p

7.创建oracle安装目录

如下目录,根据自己的实际情况可做修改:

[root@oracledb ~]# mkdir -p /data/app/oracle/product/11.2.0

[root@oracledb ~]# cd /data/app/oracle/

[root@oracledb oracle]# mkdir oradata inventory fast_recovery_area

[root@oracledb ~]# chown -R oracle:oinstall /data/app/oracle

[root@oracledb ~]# chmod -R 775 /data/app/oracle

8.配置oracle用户环境变量

编辑配置文件vi /home/oracle/.bash_profile添加如下内容:

export ORACLE_BASE=/data/app/oracle

export ORACLE_HOME=$ORACLE_BASE/product/11.2.0

export ORACLE_SID=orcl

export PATH=$PATH:$ORACLE_HOME/bin

9. 解压下载好的两个Oracle数据库文件

[root@oracledb ~]# cd /opt

[root@oracledb ~]# unzip linux.x64_11gR2_database_1of2.zip -d /data

[root@oracledb ~]# unzip linux.x64_11gR2_database_2of2.zip -d /data

10. 编辑静默安装响应文件

(1)切换到oracle用户执行

[root@oracledb ~]# su - oracle

(2)切换到响应文件的目录下

[oracle@oracledb ~]$ cp -R /data/database/response/ .

[oracle@oracledb ~]$ cd response/

[oracle@oracledb response]$ ls

dbca.rsp db_install.rsp netca.rsp

11.修改配置文件:vi /home/oracle/response/db_install.rsp,修改以下变量的值

oracle.install.option=INSTALL_DB_SWONLY

UNIX_GROUP_NAME=oinstall

INVENTORY_LOCATION=/data/app/oracle/inventory

SELECTED_LANGUAGES=en,zh_CN

ORACLE_HOME=/data/app/oracle/product/11.2.0

ORACLE_BASE=/data/app/oracle

oracle.install.db.InstallEdition=EE

oracle.install.db.DBA_GROUP=dba

oracle.install.db.OPER_GROUP=dba

DECLINE_SECURITY_UPDATES=true

12. 开始静默安装Oracle 11g

(1)开始安装oracle数据库

[oracle@oracledb ~]$ cd /data/database

[oracle@oracledb ~]$ ./runInstaller -silent -responseFile /home/oracle/response/db_install.rsp -ignorePrereq

注意:安装期间可以使用tail命令查看oracle的安装日志,且日志文件名称是根

据自己的实际执行时间确定的,安装日志文件会在安装时提示,需要等待3分钟左右,注意不要停止运行;安装完成后有如下提示,如果有类似如下提示,说明安装完成。

The following configuration scripts need to be executed as the "root" user.

#!/bin/sh

#Root scripts to run

/data/app/oraInventory/orainstRoot.sh

/data/app/oracle/product/11.2.0/db_1/root.sh

To execute the configuration scripts:

1. Open a terminal window

2. Log in as "root"

3. Run the scripts

4. Return to this window and hit "Enter" key to continue

Successfully Setup Software.

13.使用 root用户执行脚本

(1)切换到root用户执行

[root@oracledb ~]# su - root

(2)执行两个用户脚本

[root@oracledb ~]# sh /data/app/oracle/inventory/orainstRoot.sh

[root@oracledb ~]# sh /data/app/oracle/product/11.2.0/root.sh

14.配置监听程序

(1)切换到oracle用户执行(建议退出重进oracle用户)

[root@oracledb ~]# su - oracle

(2)配置监听,查看输出结果

[oracle@oracledb ~]$ netca /silent /responsefile /home/oracle/response/netca.rsp

(3)查看监听服务状态,若没启动需要启动

[oracle@oracledb ~]$ lsnrctl status

[oracle@oracledb ~]$ lsnrctl start

查看结果:

Parsing command line arguments:

Parameter "silent" = true

Parameter "responsefile" = /data/etc/netca.rsp

Done parsing command line arguments.

Oracle Net Services Configuration:

Profile configuration complete.

Oracle Net Listener Startup:

Running Listener Control:

/data/app/oracle/product/11.2.0/bin/lsnrctl start LISTENER

Listener Control complete.

Listener started successfully.

Listener configuration complete.

Oracle Net Services configuration successful. The exit code is 0

(4)查看监听端口

[oracle@oracledb ~]$ netstat -tnpl | grep 1521

15.静默创建数据库

GDBNAME = "orcl"

SID = "orcl"

SYSPASSWORD = "oracle"

SYSTEMPASSWORD = "oracle"

SYSMANPASSWORD = "oracle"

DBSNMPPASSWORD = "oracle"

DATAFILEDESTINATION =/data/app/oracle/oradata

RECOVERYAREADESTINATION=/data/app/oracle/fast_recovery_area

CHARACTERSET = "ZHS16GBK"

TOTALMEMORY = " 6552 "

##其中TOTALMEMORY = " 6552 " 为6552 MB,物理内存8G*80%

(2)切换到oracle用户执行

[root@oracledb ~]# su - oracle

(3)执行静默建库

[oracle@oracledb ~]$ dbca -silent -responseFile /home/oracle/response/dbca.rsp

执行过程如下:

Copying database files

1% complete

3% complete

11% complete

18% complete

26% complete

37% complete

Creating and starting Oracle instance

40% complete

45% complete

50% complete

55% complete

56% complete

60% complete

62% complete

Completing Database Creation

66% complete

70% complete

73% complete

85% complete

96% complete

100% complete

Look at the log file "/data/app/oracle/cfgtoollogs/dbca/orcl/orcl.log" for further details.

(4)查看进程

[oracle@oracledb ~]$ ps -ef | grep ora_ | grep -v grep

(5)再次查看监听状态进行确认

[oracle@oracledb ~]$ lsnrctl status

结果显示:

The command completed successfully

16.至此数据库就安装成功了,下面我们登录下数据库

(1)切换到oracle用户执行

[root@oracledb ~]# su - oracle

(2)登录数据库

[oracle@oracledb ~]$ sqlplus / as sysdba

(3)查询实例状态

SQLselect status from v$instance;

(4)查看数据库版本

SQL select * from v$version;

(5)激活scott用户

SQLalter user scott account unlock;

SQLalter user scott identified by tiger;

SQLselect username,account_status from dba_users;

17.设置Oracle开机启动

(1)修改配置文件vi /data/app/oracle/product/11.2.0/bin/dbstart 修改如下内容:

将脚本中的ORACLE_HOME_LISTNER=$1,修改为ORACLE_HOME_LISTNER=$ORACLE_HOME

(2)修改配置文件vi /data/app/oracle/product/11.2.0/bin/dbshut 修改如下内容:

将脚本中的ORACLE_HOME_LISTNER=$1,修改为ORACLE_HOME_LISTNER=$ORACLE_HOME

(3)修改配置文件 vi /etc/oratab修改如下内容,

orcl:/data/app/oracle/product/11.2.0:Y

(4)新建文件:vi /etc/rc.d/init.d/oracle 注意:建议全部复制脚本内容

#! /bin/bash

# oracle: Start/Stop Oracle Database 11g R2

# chkconfig: 345 90 10

# description: The Oracle Database is an Object-Relational Database Management System.

# processname: oracle

. /etc/rc.d/init.d/functions

LOCKFILE=/var/lock/subsys/oracle

ORACLE_HOME=/data/app/oracle/product/11.2.0

ORACLE_USER=oracle

case "$1" in

start)

if [ -f $LOCKFILE ]; then

echo $0 already running.

exit 1

fi

echo -n #34;Starting Oracle Database:"

su - $ORACLE_USER -c "$ORACLE_HOME/bin/lsnrctl start"

su - $ORACLE_USER -c "$ORACLE_HOME/bin/dbstart $ORACLE_HOME"

su - $ORACLE_USER -c "$ORACLE_HOME/bin/emctl start dbconsole"

touch $LOCKFILE

;;

stop)

if [ ! -f $LOCKFILE ]; then

echo $0 already stopping.

exit 1

fi

echo -n #34;Stopping Oracle Database:"

su - $ORACLE_USER -c "$ORACLE_HOME/bin/lsnrctl stop"

su - $ORACLE_USER -c "$ORACLE_HOME/bin/dbshut"

su - $ORACLE_USER -c "$ORACLE_HOME/bin/emctl stop dbconsole"

rm -f $LOCKFILE

;;

restart)

$0 stop

$0 start

;;

status)

if [ -f $LOCKFILE ]; then

echo $0 started.

else

echo $0 stopped.

fi

;;

*)

echo "Usage: $0 [start|stop|status]"

exit 1

esac

exit 0

(5)给 /etc/init.d/oracle添加执行权限

[root@oracledb ~]# chmod +x /etc/init.d/oracle

(6)开机启动oracle

[root@oracledb ~]# chkconfig oracle on

(7)给启动文件加权限

[root@oracledb ~]# cd /data/app/oracle/product/11.2.0/bin/

[root@oracledb bin]# chmod 6751 oracle

[root@oracledb bin]# cd /var/tmp

[root@oracledb tmp]# chown -R oracle:oinstall .oracle

(8)重启测试

[root@oracledb ~]# reboot

(9)切换到oracle用户登录状态下

[root@oracledb ~]# su - oracle

[oracle@oracledb ~]$ sqlplus / as sysdba

(10)解除锁定

SQL alter user SYSTEM account unlock;

(11)这时候就可以用Navicat连接了

SQL conn;

用户名:SYSTEM

密码:oracle

如果不能正确连接,改下密码试试

alter user system identified by 新密码

[root@oracledb ~]# mkdir -p /data/szzt

[root@oracledb ~]# cd /data/

[root@oracledb data]# chown oracle:oinstall -R szzt

[root@oracledb data]# ll

total 0

drwxr-xr-x 3 root root 20 Apr 8 21:14 app

drwxr-xr-x 8 root root 128 Aug 21 2009 database

drwxr-xr-x 2 oracle oinstall 6 Apr 14 16:17 szzt

[root@oracledb ~]# su - oracle

[oracle@oracledb ~]$ sqlplus / as sysdba

SQL conn cshi

Enter password: 登录失败

Connected.

SQL create temporary tablespace DB_DATA_TMP tempfile'/data/szzt/DB_DATA_TMP.dbf' size 500m autoextend on next 100m maxsize 10240m extent management local;

显示结果:

Tablespace created.

SQL create tablespace DB_DATA logging datafile'/data/szzt/DB_DATA.dbf' size 2048m autoextend on next 100m maxsize 10240m extent management local;

显示结果:

Tablespace created.

SQL create user ceshi identified by ceshi default tablespace DB_DATA temporary tablespace DB_DATA_TMP;

显示结果:

User created.

SQL grant dba to ceshi;

显示结果:

Grant succeeded.

SQL conn ceshi ###用户名和密码一样,此时登录成功

[oracle@oracledb ~]$ cd /data/app/oracle/product/11.2.0/bin/

注意:数据库导出操作是在 源服务器 上进行

[oracle@oracledb bin]$ exp 用户名/密码@ IP地址 /orcl file =/data/szzt/ceshi.dmp owner='用户名'

Export terminated successfully without warnings.

注意:数据库导入操作是在 目标服务器 上进行

[oracle@oracledb bin]$ imp ceshi/ceshi@ 10.100.1.10 /orcl file=/data/szzt/ceshi.dmp fromuser=ceshi touser=ceshi

显示结果:可以看到导入的表过程(10.100.1.10是自己的目标数据库服务器)

Import terminated successfully without warnings .

(1)、数据导入完毕后,我们可以使用DBeaver对数据库进行远程管理

(2)、测试连接,没问题后完成新建

(3)、连接上来后,我们就可以使用工具来进行管理数据库

oracle创建包?和存储过程

--创建包头

CREATE OR REPLACE PACKAGE ELS.pkg_proxy

AS

TYPE cursortype IS REF CURSOR;

-- 得到交接确认代理人数据

PROCEDURE get_proxy_info (

v_emp_no IN VARCHAR2, --当前登录用户

v_to_emp_no IN VARCHAR2, --代理人工号

v_to_emp_name IN VARCHAR2, --代理人姓名

v_proxy_date1 IN VARCHAR2, --代理开始时间: yyyy/mm/dd

v_proxy_date2 IN VARCHAR2, --代理结束时间: yyyy/mm/dd

v_from_emp_no IN VARCHAR2, --被代理人工号

v_from_emp_name IN VARCHAR2, --被代理人姓名

p_cursor OUT cursortype

);

END pkg_proxy;

--创建包头

CREATE OR REPLACE PACKAGE BODY ELS.pkg_proxy

AS

-- 得到交接确认代理人数据

PROCEDURE get_proxy_info (

v_emp_no IN VARCHAR2, --当前登录用户

v_to_emp_no IN VARCHAR2, --代理人工号

v_to_emp_name IN VARCHAR2, --代理人姓名

v_proxy_date1 IN VARCHAR2, --代理开始时间: yyyy/mm/dd

v_proxy_date2 IN VARCHAR2, --代理结束时间: yyyy/mm/dd

v_from_emp_no IN VARCHAR2, --被代理人工号

v_from_emp_name IN VARCHAR2, --被代理人姓名

p_cursor OUT cursortype

)

AS

BEGIN

OPEN p_cursor FOR

WITH v AS

(

--查找当前登录人是否为管理员

SELECT *

FROM employee

WHERE dept_id IN (

SELECT ID

FROM department

START WITH ID =

(SELECT dept_id

FROM employee

WHERE emp_no = v_emp_no

AND is_admin IN (

SELECT r_value

FROM role_conditions

WHERE r_key =

'ProxySearch')

UNION

SELECT dept_id

FROM employee_add

WHERE emp_no = v_emp_no

AND is_admin IN (

SELECT r_value

FROM role_conditions

WHERE r_key =

'ProxySearch'))

CONNECT BY PRIOR ID = parent_id)

UNION

SELECT *

FROM employee

WHERE emp_no = v_emp_no)

SELECT DISTINCT *

FROM (SELECT a.currentuser, a.toagrentuser,

DECODE (a.agent_type,

'A', '当前交接确认代理',

'B', '将来离职签核代理',

'C', '当前离职签核代理',

'D', '将来交接确认代理',

''

) agent_type,

a.from_date, a.TO_DATE,

DECODE

(a.to_date1,

'', '无',

TO_CHAR (a.TO_DATE,

'yyyy/mm/dd am hh:mi:ss'

)

) to_date1,

a.flowinfo, b.emp_cname AS currentusername,

b1.emp_cname AS toagrentusername, workitem

FROM (

--当前交接确认代理

SELECT SUBSTR

(x.currentuser,

1,

INSTR (x.currentuser, '(') - 1

) AS currentuser,

SUBSTR

(x.toagrentuser,

1,

INSTR (x.toagrentuser, '(') - 1

) AS toagrentuser,

'A' AS agent_type,

x.create_date AS from_date,

x.create_date AS TO_DATE,

NULL AS to_date1, x.workid,

NULL AS formid, y.NAME workitem,

(SELECT e.emp_no

|| '('

|| e.emp_cname

|| ')'

FROM employee e

WHERE e.emp_no = y.emp_no)

AS flowinfo

FROM currentworkagrent x,

worktransferitem_m y

WHERE x.workid = y.ID

UNION ALL

--将来交接确认代理

SELECT SUBSTR

(currentuser,

1,

INSTR (currentuser, '(') - 1

) AS currentuser,

SUBSTR

(toagrent,

1,

INSTR (toagrent, '(') - 1

) AS toagrent,

'D' AS agent_type, starttime,

endtime, endtime AS to_date1, NULL,

NULL, NULL, NULL

FROM futureagrent) a,

employee b,

employee b1

WHERE a.currentuser = b.emp_no(+)

AND a.toagrentuser = b1.emp_no(+)) a

WHERE currentuser IN (SELECT emp_no

FROM v)

AND (currentuser = v_from_emp_no OR v_from_emp_no IS NULL

)

AND ( currentusername = v_from_emp_name

OR v_from_emp_name IS NULL

)

AND (toagrentuser = v_to_emp_no OR v_to_emp_no IS NULL)

AND ( toagrentusername = v_to_emp_name

OR v_to_emp_name IS NULL

)

AND from_date =

TO_DATE (NVL (v_proxy_date2, '2099/12/31'),

'yyyy/mm/dd'

)

AND TO_DATE =

TO_DATE (NVL (v_proxy_date1, '1000/12/31'),

'yyyy/mm/dd'

)

ORDER BY a.agent_type, a.from_date DESC;

END;

END pkg_proxy;


分享标题:oracle如何建包,oracle怎么创建包
当前URL:http://cdiso.cn/article/hosegd.html

其他资讯