search
HomeDatabaseMysql TutorialOracle Scheduler Jobs

创建Jobs语法: 通过DBMS_SCHEDULER包来创建Jobs,是使用其CREATE_JOB过程。例如: SQLgt; BEGIN 2 DBMS_SCHEDULER.CRE

创建Jobs语法:

通过DBMS_SCHEDULER包来创建Jobs,是使用其CREATE_JOB过程。例如:

事实上,有权限的话,用户也可以创建其它SCHEMA下的JOB,只需要在指定JOB_NAME时,按照schema.job_name的格式即可。

上例中指定的参数,分别代表的含义如下:

  • JOB_NAME :指定任务的名称,必选值,注意要确保指定的名称唯一。
  • JOB_TYPE :任务执行的操作类型,必选值,有下列几个可选值:
  • PLSQL_BLOCK :表示任务执行的是一个PL/SQL匿名块。
  • STORED_PROCEDURE :表示任务执行的是Oracle过程(含PL/SQL PROCEDURE和JAVA PROCEDURE),本例中正是指定这一参数值。
  • EXECUTABLE :表示任务执行的是一个外部程序,比如说操作系统命令。
  • CHAIN :表示任务执行的是一个CHAIN。
  • JOB_ACTION :任务执行的操作,必选值,应与JOB_TYPE类型中指定的参数相匹配。比如说对于PL/SQL匿名块,此处就可以放置PL/SQL块的具体代表,类似DECLARE .. BEGIN ..END这类;如果是ORACLE过程,那么此处应该指定具体的过程名,注意由于任务执行,即使过程中有OUT之类参数,实际执行时也不会有输出的。
  • START_DATE :指定任务初次执行的时间,本参数可为空,当为空时,表示任务立刻执行,效果等同于指定该参数值为SYSDATE。
  • REPEAT_INTERVAL :指定任务执行的频率,比如多长时间会被触发再次执行。本参数也可以为空,如果为空的话,就表示当前设定的任务只执行一次。REPEAT_INTERVAL参数需要好好说说,REPEAT_INTERVAL参数的语法结构要复杂的多。其中最重要的是FREQ和INTERVAL两个关键字。
  • FREQ 关键字用来指定间隔的时间周期,可选参数有:YEARLY, MONTHLY, WEEKLY, DAILY, HOURLY, MINUTELY, and SECONDLY,分别表示年、月、周、日、时、分、秒等单位。
  • INTERVAL 关键字用来指定间隔的频繁,可指定的值的范围从1-99。
  • 例如:REPEAT_INTERVAL=>'FREQ=DAILY;INTERVAL=1';表示每天执行一次,如果将INTERVAL改为7就表示每7天执行一次,效果等同于FREQ=WEEKLY;INTERVAL=1。

    一般来说,使用DBMS_SCHEDULER.CREATE_JOB创建一个JOB,至少需要指定上述参数中的前3项。除此之外,还可以在CREATE_JOB时,指定下列参数:

  • NUMBER_OF_ARGUMENTS :指定该JOB执行时需要附带的参数的数量,默认值为0,注意当JOB_TYPE列值为PLSQL_BLOCK或CHAIN时,本参数必须设置为0,因为上述两种情况下不支持附带参数。
  • END_DATE :指定任务的过期时间,默认值为NULL。任务过期后,,任务的STATE将自动被修改为COMPLETED,ENABLED被置为FALSE。如果该参数设置为空的话,表示该任务永不过期,将一直按照REPEAT_INTERVAL参数设置的周期重复执行,直到达到设置的MAX_RUNS或MAX_FAILURES值。
  • JOB_CLASS :指定任务关联的CLASS,默认值为DEFAULT_JOB_CLASS。关于JOB CLASS的信息就关注本系列的后续文章。
  • ENABLED :指定任务是否启用,默认值为FALSE。FALSE状态表示该任务并不会被执行,除非被用户手动调用,或者用户将该任务的状态修改为TRUE。
  • AUTO_DROP :当该标志被置为TRUE时,ORACLE会在满足条件时自动删除创建的任务
  • 任务已过期;
  • 任务最大运行次数已达MAX_RUNS的设置值;
  • 任务未指定REPEAT_INTERVAL参数,仅运行一次;
  • COMMENTS :设置任务的注释信息,默认值为NULL。

  • 例子:

    首先创建一个存储过程,是向test表里面插入一条记录。

    下面创建jobs

    不过,细心的盆友可能会发现,JOB虽然成功创建了,但却并未执行,这是怎么回事?其实原因很简单,还记的前面介绍CREATE_JOB过程时提到的ENABLED参数吗,当不显式指定时,该参数的默认值为false,JOB自然不会运行了。如果遇到这类情形,如何修改呢?

    修改jobs

    SET_ATTRIBUTE 过程虽然仅有三个参数,不过能够修改的属性值可是不少,以下列举几个较常用到的:

  • LOGGING_LEVEL :指定对jobs执行情况记录的日志信息级别。

    SCHEDULER 管理的JOB对任务的执行情况专门进行了记录,同时用户还可以选择日志中记录信息的级别,有下列三种选择:

  • DBMS_SCHEDULER.LOGGING_OFF :关闭日志记录功能;
  • DBMS_SCHEDULER.LOGGING_RUNS :对任务的运行信息进行记录;
  • DBMS_SCHEDULER.LOGGING_FULL :记录任务所有相关信息,不仅有任务的运行情况,甚至连任务的创建、修改等也均将记入日志。
  • 提示:查看SCHEDULER管理的JOB,可以通过USER_SCHEDULER_JOB_LOG和USER_SCHEDULER_JOB_RUN_DETAILS两个视图中查询

  • RESTARTABLE :指定jobs运行出错后,是否能够适时重启。创建任务时如未明确指定,本参数默认情况下设置为FALSE,如果设置为TRUE,就表示当任务运行时出错,下次运行时间点到达时仍会启动,并且如果运行仍然出错,会继续重新运行,不过如果连接出错达到6次,该job就会停止。
  • MAX_FAILURES :指定jobs最大连续出错次数。该参数值可指定的范围从1-1000000,默认情况下该参数设置为NULL,表示无限制。达到指定出错次数后,该job会被自动disable。
  • MAX_RUNS :指定jobs最大运行次数。该参数值可指定的范围从1-1000000,默认情况下该参数设置为NULL,表示无限制(只是运行次数无限制,实际job会否继续运行,仍受制于end_date以及max_failures等参数的设置)。达到指定运行次数后,该job也将被自动disable,并且状态会被置为COMPLETED。
  • JOB_TYPE :指定job执行的任务的类型。有四个可选值:¨PLSQL_BLOCK¨, ¨STORED_PROCEDURE¨, ¨EXECUTABLE¨, and ¨CHAIN¨。
  • JOB_ACTION :指定job执行的任务。这一参数所指定的值依赖于JOB_TYPE参数中的值,比如说JOB_TYPE设置为¨STORED_PROCEDURE¨,那么本参数值中指定的一定是ORACLE中的过程名。
  • START_DATE :指定job初次启动的时间
  • END_DATE :指定job停止运行的时间。本参数又与AUTO_DROP相关联,如果AUTO_DROP设置为TRUE的话,那么一旦job到达停止运行的时间,该job就会被自动删除,否则的话job一直存在,不过状态被修改为COMPLETED。
  • 上述提到了这些参数,均可以使用DBMS_SCHEDULER.SET_ATTRIBUTE过程进行设置。另外需要注意一点,除了用户手动创建的jobs之外,数据库在运行过程中也有可能自动创建jobs。对于这类jobs除非必要,否则不建议进行修改。至于如何区分jobs是用户创建,还是数据库自动创建,可以通过*_SCHEDULER_JOBS视图的SYSTEM列来确定,如果该列显示为TRUE,则表示由系统创建。

    Statement
    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
    Explain the role of InnoDB redo logs and undo logs.Explain the role of InnoDB redo logs and undo logs.Apr 15, 2025 am 12:16 AM

    InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

    What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?Apr 15, 2025 am 12:15 AM

    Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

    What is the Using temporary status in EXPLAIN and how to avoid it?What is the Using temporary status in EXPLAIN and how to avoid it?Apr 15, 2025 am 12:14 AM

    Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

    Describe the different SQL transaction isolation levels (Read Uncommitted, Read Committed, Repeatable Read, Serializable) and their implications in MySQL/InnoDB.Describe the different SQL transaction isolation levels (Read Uncommitted, Read Committed, Repeatable Read, Serializable) and their implications in MySQL/InnoDB.Apr 15, 2025 am 12:11 AM

    MySQL/InnoDB supports four transaction isolation levels: ReadUncommitted, ReadCommitted, RepeatableRead and Serializable. 1.ReadUncommitted allows reading of uncommitted data, which may cause dirty reading. 2. ReadCommitted avoids dirty reading, but non-repeatable reading may occur. 3.RepeatableRead is the default level, avoiding dirty reading and non-repeatable reading, but phantom reading may occur. 4. Serializable avoids all concurrency problems but reduces concurrency. Choosing the appropriate isolation level requires balancing data consistency and performance requirements.

    MySQL vs. Other Databases: Comparing the OptionsMySQL vs. Other Databases: Comparing the OptionsApr 15, 2025 am 12:08 AM

    MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

    How does MySQL index cardinality affect query performance?How does MySQL index cardinality affect query performance?Apr 14, 2025 am 12:18 AM

    MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

    MySQL: Resources and Tutorials for New UsersMySQL: Resources and Tutorials for New UsersApr 14, 2025 am 12:16 AM

    The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

    Real-World MySQL: Examples and Use CasesReal-World MySQL: Examples and Use CasesApr 14, 2025 am 12:15 AM

    MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

    See all articles

    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

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. How to Fix Audio if You Can't Hear Anyone
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    WWE 2K25: How To Unlock Everything In MyRise
    1 months agoBy尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    Safe Exam Browser

    Safe Exam Browser

    Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

    SublimeText3 Linux new version

    SublimeText3 Linux new version

    SublimeText3 Linux latest version

    MantisBT

    MantisBT

    Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

    WebStorm Mac version

    WebStorm Mac version

    Useful JavaScript development tools