Home Database Mysql Tutorial Oracle通过job定时执行任务

Oracle通过job定时执行任务

Jun 07, 2016 pm 05:31 PM
or

在项目中,经常会遇到需要定时完成的任务,比如定时更新数据,定义统计数据生成报表等等,其实这些事情都可以使用Oracle的Job来完

  在项目中,经常会遇到需要定时完成的任务,比如定时更新数据,定义统计数据生成报表等等,其实这些事情都可以使用Oracle的Job来完成。下面考试大就结合我们实验室项目实际,简单介绍一下在Oracle数据库中通过Job完成自动创建表的方法。

  整个过程总共分为两步。虽然整个过程都非常简单,但是对于初学Oracle的生手还是有很多地方需要注意的。

  首先介绍一下,创建该JOB的背景,因为每天更新的直播和点播节目信息比较多,为了方便处理,需要每天创建一张表来记录更新的节目信息,,当前数据库中已经有一张tbl_programme的表,每天创建的表的字段需要同tbl_programme保持一致,每天新创建的表的名称格式为tbl_programme_日期(例如:tbl_programme_20090214)规定每天晚上1点钟生成该天的新表。

相关阅读:

关于Oracle RAC中的job运行在哪个实例的理解

Oracle job不执行的分析处理

Oracle job interval规则

学习Oracle环境中job创建

  第一步:创建一个执行创建操作的存储过程

  在这一步首先要解决的问题就是构造表名。在Oracle中格式化输出时间可以用to_char函数来处理,例如:

  SQL> select to_char(sysdate, ’yyyy/mm/dd hh24:mi:ss’) from dual;
  TO_CHAR(SYSDATE,’YYYY/MM/DDHH2
  ------------------------------
  2009/02/14 17:22:41
  --以上SQL格式化输出了时间,要得到我们所需要的格式直接修改一下SQL即可
  SQL> select to_char(sysdate, ’yyyymmdd’) from dual;
  TO_CHAR(SYSDATE,’YYYYMMDD’)
  ---------------------------
  20090214

  得到时间格式字符串后我们就可以将表名的前缀和时间连接在一起形成完整的表名。这里需要注意,在Oracle中连接两个字符串需要使用‘||’符号,而在Sql Server中直接使用‘+’号就可以了,因为我以前一直在Sql Server下编程,好久都没编写Oracle的SQL所以费了很大的功夫才发现这个问题。完整的Sql就是

  SQL> select ’tbl_programme_’ || to_char(sysdate, ’yyyymmdd’) from dual;

  ’TBL_PROGRAMME_’||TO_CHAR(SYSD

  ------------------------------

  tbl_programme_20090214

  接下来就是创建表的代码了,因为新表需要tbl_programme保持一致,所以直接CTAS来创建表那是非常适合的了,代码如下:

  Create table tablename as select * from tbl_programme

  如果需要指定一个TableSpace则将该SQL做适当修改:

  Create table tablename tablespace p2p as select * from tbl_programme

  所以整个创建存储过程的SQL就是

  create or replace procedure sp_createtab_tbl_programme
  Authid Current_User
  as
  tabname varchar(200);
  begin
  select ’TBL_PROGRAMME_’ || to_char(sysdate, ’yyyymmdd’) into tabname fromdual;
  --create table tabname as select * from tbl_programme where 1 != 1;
  execute immediate ’create table ’ || tabname ||’ tablespace p2p as select* from tbl_programme where 1 != 1’;
  commit;
  end;
  /

  这里还需要注意一下在Oracle里面如果要对一个变量赋值的话有两种方式:

  (1)使用:=进行赋值

  (2)使用select ‘xjkxj ’ into 变量名称 from tabname

  另外,在存储过程中定义变量的时候一般放在as/is后begin前面。在存储过程一般是不能直接使用create table,truncate table这类似的语句的,如果要使用这些语句必须使用excute immediate + 所要执行的sql语句来实现。

  注意上面用红色标志的语句:Authid Current_User

  这个语句比较重要,如果我们在创建存储过程的时候不添加这条语句执行该存储过程将不会成功,原因是默认情况向存储过程是没有Create table等权限的,即使当前用户有DBA的权限也不行,如果存储过程中存在创建表的操作,可以有以下两种方式来解决该问题。

  (1)显示的赋予该用户Create table的权限,grant create table to user.

  (2)在存储过程中使用Authid Current_User 标识使用当前用户的权限。

linux

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

When might a full table scan be faster than using an index in MySQL? When might a full table scan be faster than using an index in MySQL? Apr 09, 2025 am 12:05 AM

Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

Explain InnoDB Full-Text Search capabilities. Explain InnoDB Full-Text Search capabilities. Apr 02, 2025 pm 06:09 PM

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

Can I install mysql on Windows 7 Can I install mysql on Windows 7 Apr 08, 2025 pm 03:21 PM

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

Difference between clustered index and non-clustered index (secondary index) in InnoDB. Difference between clustered index and non-clustered index (secondary index) in InnoDB. Apr 02, 2025 pm 06:25 PM

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values ​​and pointers to data rows, and is suitable for non-primary key column queries.

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

How do you handle large datasets in MySQL? How do you handle large datasets in MySQL? Mar 21, 2025 pm 12:15 PM

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

The relationship between mysql user and database The relationship between mysql user and database Apr 08, 2025 pm 07:15 PM

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

See all articles