Oracle中如何写脚本 oracle编写

oracle 如何编写定时脚本用jobOracle中如何写脚本,具体用法Oracle中如何写脚本:
declare
n_job binary_integer;
begin
dbms_job.submit(n_job, '你要执行Oracle中如何写脚本的东西', sysdate, TRUNC(LAST_DAY(SYSDATE)) 4 2/24);
end;
-- 每月4号执行
dbms_job.submit(n_job_01,'你调Oracle中如何写脚本的东西',sysdate,'TRUNC(LAST_DAY(SYSDATE)) 4 2/24');
-- 每分钟执行一次
dbms_job.submit(n_job_01,'你调Oracle中如何写脚本的东西',sysdate,'TRUNC(sysdate,’mi’)1 / (24*60)');
-- 凌晨两点执行
dbms_job.submit(n_job_01,'你调的东西',sysdate,'TRUNC(sysdate)12/24');
-- 每周一凌晨2点执行 周一是每周的第二天next_day(sysdate,2)同理周二是第三天,next_day(sysdate,3)
dbms_job.submit(n_job_01,'你调的东西',sysdate,'TRUNC(next_day(sysdate,2)) 2/24');
-- 每月1日凌晨两点执行
dbms_job.submit(n_job_01,'你调的东西',sysdate,'TRUNC(LAST_DAY(SYSDATE)) 1 2/24');
-- 每季第一天凌晨两点执行
dbms_job.submit(n_job_01,'你调的东西',sysdate,'TRUNC(ADD_MONTHS(SYSDATE,3),'Q')2/24');
-- 每年7月1日和1月1日凌晨2点
dbms_job.submit(n_job_01,'你调的东西',sysdate,'ADD_MONTHS(trunc(sysdate,'yyyy'),6) 2/24');
-- 每年1月1日凌晨2点执行
dbms_job.submit(n_job_01,'你调的东西',sysdate, 'Add_months(trunc(sysdate,'yyyy'), 12)2/24');
oracle insert into 脚本怎么写INSERT INTO BOOK(bookid,name,price)VALUES('100123','oracle ',54);
或者
INSERT INTO tablename(bookid,name,price)
SELECT '100123','oracle,54 FROM DUAL;
总结下Oracle 中的Insert用法
1.标准Insert --单表单行插入
语法:
INSERT INTO table [(column1,column2,...)] VALUE (value1,value2,...)
例子:
insert into dep (dep_id,dep_name) values(1,'技术部');
2, 无条件 Insert all --多表多行插入
语法:
INSERT [ALL] [condition_insert_clause]
[insert_into_clause values_clause] (subquery)
示例:
INSERT ALL
INTO sal_history(emp_id,hire_date,salary) values (empid,hiredate,sal)
INTO mgr_history(emp_id,manager_id,salary) values (empid,hiredate,sal)
SELECT employee_id empid,hire_date hiredate,salary sal,manager_id mgr
FROM employees
WHERE employee_id200;
3,有条件的Insert
语法:
INSERT [ALL | FIRST]
WHEN condition THEN insert_into_clause values_clause
[WHEN condition THEN] [insert_into_clause values_clause]
......
[ELSE] [insert_into_clause values_clause]
Subquery;
示例:
Insert All
when id5 then into z_test1(id, name) values(id,name)
when id2 then into z_test2(id) values(id)
else into z_test3 values(name)
select id,name from z_test;
4, 旋转Insert (pivoting insert)
create table sales_source_data (
employee_id number(6),
week_id number(2),
sales_mon number(8,2),
sales_tue number(8,2),
sales_wed number(8,2),
sales_thur number(8,2),
sales_fri number(8,2)
);
insert into sales_source_data values (176,6,2000,3000,4000,5000,6000);
create table sales_info (
employee_id number(6),
week number(2),
sales number(8,2)
);
示例如下:
insert all
into sales_info values(employee_id,week_id,sales_mon)
into sales_info values(employee_id,week_id,sales_tue)
into sales_info values(employee_id,week_id,sales_wed)
into sales_info values(employee_id,week_id,sales_thur)
into sales_info values(employee_id,week_id,sales_fri)
select employee_id,week_id,sales_mon,sales_tue,
sales_wed,sales_thur,sales_fri
from sales_source_data;
oracle insert into 脚本怎么写?INSERT INTO BOOK(bookid,name,price)VALUES('100123','oracle ',54);
或者
INSERT INTO tablename(bookid,name,price)
SELECT '100123','oracle,54 FROM DUAL;
总结下Oracle 中的Insert用法
1.标准Insert --单表单行插入
语法:
INSERT INTO table [(column1,column2,...)] VALUE (value1,value2,...)
例子:
insert into dep (dep_id,dep_name) values(1,'技术部');
2, 无条件 Insert all --多表多行插入
语法:
INSERT [ALL] [condition_insert_clause]
[insert_into_clause values_clause] (subquery)
示例:
INSERT ALL
INTO sal_history(emp_id,hire_date,salary) values (empid,hiredate,sal)
INTO mgr_history(emp_id,manager_id,salary) values (empid,hiredate,sal)
SELECT employee_id empid,hire_date hiredate,salary sal,manager_id mgr
FROM employees
WHERE employee_id200;
3,有条件的Insert
语法:
INSERT [ALL | FIRST]
WHEN condition THEN insert_into_clause values_clause
[WHEN condition THEN] [insert_into_clause values_clause]
......
[ELSE] [insert_into_clause values_clause]
Subquery;
示例:
Insert All
when id5 then into z_test1(id, name) values(id,name)
when id2 then into z_test2(id) values(id)
else into z_test3 values(name)
select id,name from z_test;
4, 旋转Insert (pivoting insert)
create table sales_source_data (
employee_id number(6),
week_id number(2),
sales_mon number(8,2),
sales_tue number(8,2),
sales_wed number(8,2),
sales_thur number(8,2),
sales_fri number(8,2)
);
insert into sales_source_data values (176,6,2000,3000,4000,5000,6000);
create table sales_info (
employee_id number(6),
week number(2),
sales number(8,2)
);
示例如下:
insert all
into sales_info values(employee_id,week_id,sales_mon)
into sales_info values(employee_id,week_id,sales_tue)
into sales_info values(employee_id,week_id,sales_wed)
into sales_info values(employee_id,week_id,sales_thur)
into sales_info values(employee_id,week_id,sales_fri)
select employee_id,week_id,sales_mon,sales_tue,
sales_wed,sales_thur,sales_fri
from sales_source_data;
Oracle数据库备份脚本怎么写?没必要用root权限执行,oracle用户同样可以执行crontab计划任务
1、su - oracle
crontab -e
0 4 * * 0 /home/oracle/exp.sh 21 /home/oracle/script_exp.log;
0~59 表示分
0~23 表示小时
1~31 表示日
1~12 表示月份
0~6 表示星期(其中0表示星期日)
2、cat /home/oracle/exp.sh
. ~/.bash_profile
bakdir=/opt/dbbak
logdir=/home/oracle/dbbak
rq=$(date%Y%m%d)
exp system/***** owner=jysh file=${bakdir}/jysh_${rq}.dmp log=${logdir}/jysh_${rq}.log;
find ${bakdir} -type f -mtime30 -exec rm -rf {} \;
oracle rman备份脚本怎么写在ORACLE数据库中,RMAN备份的脚本非常多 , 下面介绍一例shell脚本如何通过RMAN备份 , 以及FTP上传RMAN备份文件以及归档日志文件的脚本 。
fullback.sh 里面调用RMAN命令做数据库备份,它使用的cmdfile为/home/oracle/backup/bin/fullback.rcv , 同时在/home/oracle/backup/logs目录下生成日志文件 。
1: [oracle@DB-Server bin]$ more fullback.sh
2:
3: #!/bin/bash
4:
5: export ORACLE_BASE=/u01/app/oracle
6:
7: export ORACLE_SID=gps
8:
9: ORACLE_HOME=$ORACLE_BASE/product/10.2.0/db_1; export ORACLE_HOME
10:
11: TMP=/tmp; export TMP
12:
13: TMPDIR=$TMP; export TMPDIR
14:
15: PATH=/usr/sbin:$PATH; export PATH
16:
17: PATH=$ORACLE_HOME/bin:$PATH; export PATH
18:
19: LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib; export LD_LIBRARY_PATH
20:
21: CLASSPATH=$ORACLE_HOME/JRE:$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib;
22:
23: export CLASSPATH
24:
25: TODAY=`date%Y_%m_%d`
26:
27: rman nocatalog target / cmdfile /home/oracle/backup/bin/fullback.rcv log /home/oracle/backup/logs/fullbackup_$TODAY.log
28:
29: /home/oracle/backup/bin/ftpbackup.sh
30:
fullback.rcv文件非常简单,如下所示:
1: [oracle@DB-Server bin]$ more /home/oracle/backup/bin/fullback.rcv
2:
3: run{
4:
5: allocate channel c4 type disk;
6:
7: backup as compressed backupset
8:
9: skip inaccessible
10:
11: tag fullbackupwitharchivelog
12:
13: (database);
14:
15: backup current controlfile;
16:
17: backup spfile;
18:
19: sql "alter system archive log current";
20:
21: delete noprompt obsolete;
22:
23: release channel c4;
24:
25: }
26:
RMAN生成的备份文件,需要通过FTP上传到FTP服务器,一则数据库服务器没有这么多空间存储多天的备份,二则是出于容灾、数据安全需要 。
下面脚本中FTP服务器 , 用户名密码均使用xxx替代,在实际环境中,使用具体的信息替代即可 。
1: [oracle@DB-Server bin]$ more ftpbackup.sh
2:
3: #!/bin/sh、
4:
5: rm -f /home/oracle/.netrc
6:
7: date_yesterday=`date -d'yesterday'%Y_%m_%d`
8:
9: date_today=`date%Y_%m_%d`
10:
11: echo "default login xxxx password xxxxxx"/home/oracle/.netrc
12:
13: echo "macdef init"/home/oracle/.netrc
14:
15: echo "binary"/home/oracle/.netrc
16:
17: echo "cd archivelog"/home/oracle/.netrc
18:
19: echo "mkdir $date_yesterday"/home/oracle/.netrc
20:
21: echo "cd $date_yesterday"/home/oracle/.netrc
22:
23: echo "lcd /u04/flash_recovery_area/gps/archivelog/$date_yesterday"/home/oracle/.netrc
24:
25: echo "mput *"/home/oracle/.netrc
26:
27: echo "cd .."/home/oracle/.netrc
28:
29: echo "mkdir $date_today" /home/oracle/.netrc
30:
31: echo "cd $date_today" /home/oracle/.netrc
32:
33: echo "lcd /u04/flash_recovery_area/gps/archivelog/$date_today" /home/oracle/.netrc
34:
35: echo "mput * "/home/oracle/.netrc
36:
37: echo "cd .." /home/oracle/.netrc
38:
39: echo "cd ../backupset"/home/oracle/.netrc
40:
41: echo "mkdir $date_today"/home/oracle/.netrc
42:
43: echo "cd $date_today"/home/oracle/.netrc
44:
45: echo "lcd /u04/flash_recovery_area/gps/backupset/$date_today"/home/oracle/.netrc
46:
47: echo "mput *"/home/oracle/.netrc
48:
49: echo "cd .."/home/oracle/.netrc
50:
51: echo "cd ../autobackup"/home/oracle/.netrc
52:
53: echo "mkdir $date_today"/home/oracle/.netrc
54:
55: echo "cd $date_today"/home/oracle/.netrc
56:
57: echo "lcd /u04/flash_recovery_area/gps/autobackup/$date_today"/home/oracle/.netrc
58:
59: echo "mput *"/home/oracle/.netrc
60:
61: echo "quit"/home/oracle/.netrc
62:
63: echo ""/home/oracle/.netrc
64:
65: chmod 600 /home/oracle/.netrc
66:
67: ftp -i -v xxx.xxx.xxx.xxx 8021 /home/oracle/backup/logs/ftp$date_today.log 21
68:
另外,关于归档日志也需要每隔2小时上传一次到FTP服务器,2小时上传一次归档日志的shell脚本如下所示:
1: [oracle@DB-Server bin]$ more ftp2hours.sh
2:
3: #!/bin/sh
4:
5: rm -f /home/oracle/.netrc
6:
7: date_yesterday=`date -d'yesterday'%Y_%m_%d`
8:
9: date_today=`date%Y_%m_%d`
10:
11: echo "default login xxxx password xxxx"/home/oracle/.netrc
12:
13: echo "macdef init"/home/oracle/.netrc
14:
15: echo "binary"/home/oracle/.netrc
16:
17: echo "cd archivelog"/home/oracle/.netrc
18:
19: echo "mkdir $date_today" /home/oracle/.netrc
20:
21: echo "cd $date_today" /home/oracle/.netrc
22:
23: echo "lcd /u04/flash_recovery_area/gps/archivelog/$date_today" /home/oracle/.netrc
24:
25: echo "mput * "/home/oracle/.netrc
26:
27: echo "quit"/home/oracle/.netrc
28:
29: echo ""/home/oracle/.netrc
30:
31: chmod 600 /home/oracle/.netrc
32:
33: ftp -i -v xxx.xxx.xxx.xxx 8021 /home/oracle/backup/logs/ftp2hours.$date_today.log 21
34:
最后需要将RMAN备份生成的日志文件,以及FTP上传备份文件以及归档日志的记录通过邮件形式发送给DBA或系统管理员 ,
1: [oracle@DB-Server bin]$ more chkbackandmail.sh
2: #!/bin/bash
3: rm -f /home/oracle/backup/bin/sendmail.pl
4: date_today=`date%Y_%m_%d`
5: subject="Oracle Backup Alert Service on $date_today"
6: content="Dear colleagues,
7:
8:Attached please find the logs of xxx(xxx.xxx.xxx.xxx) oracle database backup and transfer to FTP Server(xxx.xxx.xxx.xxx), please
9:review the file and check whether the backup succeeded or not,and double check all backups have been dumped to tape, many tha
10: nks
11:
12:
13:
14:
15: Best regards
16: Oracle Alert Services
17:
18: "
19: file="/home/oracle/backup/logs/fullbackup_$date_today.log,/home/oracle/backup/logs/ftp$date_today.log"
20: echo "#!/usr/bin/perl"/home/oracle/backup/bin/sendmail.pl
21: echo "use Mail::Sender;"/home/oracle/backup/bin/sendmail.pl
22: echo "\$sender = new Mail::Sender {smtp = 'xxx.xxx.xxx.xxx', from = 'xxxx@xxx.com'}; " /home/oracle/backup/bin/sendmai
23: l.pl
24: echo "\$sender-MailFile({to = 'xxx@esquel.com'," /home/oracle/backup/bin/sendmail.pl
25: echo "cc='xxx@xxx.com,xxx@xxx.com,xxx@xxx.com',"/home/oracle/backup/b
26: in/sendmail.pl
27: echo "subject = '$subject'," /home/oracle/backup/bin/sendmail.pl
28: echo "msg = '$content'," /home/oracle/backup/bin/sendmail.pl
29: echo "file = '$file'});" /home/oracle/backup/bin/sendmail.pl
30: perl /home/oracle/backup/bin/sendmail.pl
最后在Crontab 作业里面配置调用这些shell脚本 。例如如下所示,在1:01分执行fullback.sh ,每隔两个小时(例如0:50、2:50...)执行一次ftp2hours.sh, 在每天早上8:40执行chkbackandmail.sh 发送fullback.sh 以及ftp2hour.sh的执行日志记录 。
oracle数据库,批量插入数据脚本批量插入数据脚本
1、第一种批量插入数据脚本 , 可以基本满足要求 。理解上较为简单,所以这个最常用 。
NEXTVAL和CURRVAL的区别:
1、如果 sequence.CURRVAL 和 sequence.NEXTVAL 都出现在一个 SQL 语句中,则序列只增加一次 。在这种情况下,每个 sequence.CURRVAL 和 sequence.NEXTVAL 表达式都返回相同的值,不管在语句中sequence.CURRVAL 和 sequence.NEXTVAL 的顺序 。
执行脚本结果如下:
3、两个表,同时批量插入数据的脚本
3、
4、 涉及子表时,批量插入数据脚本,
5、 批量修改数据 :
时间取数方式:
一、SYSTIMESTAMP(取当前系统值)
二、SYSDATE(取当前系统值,但只精确到时,分和秒都为0)
三、固定值为:TO_TIMESTAMP ('2019-2-12 15:24:45.703000','yyyy-mm-dd hh24:mi:ss.ff6')
把固定的字段改为变量:
方式一:’||i||’ 例:’{“no”:“111’||i||’”}’(此方式)
方式二:concat 例:concat(concat(’{“blNo”:111"’,i),’"}’)
【Oracle中如何写脚本 oracle编写】Oracle中如何写脚本的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于oracle编写、Oracle中如何写脚本的信息别忘了在本站进行查找喔 。

    推荐阅读