程序包启动时自动执行代码 [英] Automatically execute code on package startup

查看:47
本文介绍了程序包启动时自动执行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要参加考试,并且有一个说明,我不确定这是什么意思.可以给我一个提示还是给我一个简短的例子.

I have an exam coming up and have a specification which I'm not sure what it means. Can please give me someone a hint or give me a short example for it.

规范:在程序包启动时自动执行代码:

Specification: Automatically execute code on package Startup:

如果符合条件:编写的代码将自动初始化变量 无需用户的明确呼叫.

If criteria met: Code written that automatically initialises variable without explicit call from user.

我不确定是否会问到这个问题,但是是否可以这样调用程序包:PACKAGE_NAME.function_name那么没有值的调用吗?是问什么吗?

I'm not sure if this is asked but can it be that I have to call the package like this: PACKAGE_NAME.function_name so a call without a value? Is that what is asked?

如果是这样,您不需要进一步帮助我,因为我知道该怎么做.

If so, you don't need to help me further because I know what to do .

推荐答案

您可以在程序包主体中包含不属于任何私有或公共功能或过程的代码;该代码是在每个会话实例化该程序包时执行的.

You can have code in the package body that is not part of any of the private or public functions or procedures; that code is executed when the package is instantiated by each session.

作为一个人为的示例,您可能想用初始化时间填充变量,您可以使用以下方法进行初始化:

As a contrived example, you might want to populate a variable with the initialisation time, which you could do with:

create package p42 as
  procedure some_proc;
end p42;
/

create package body p42 as
  init_time timestamp;

  procedure some_proc is
  begin
    -- do something
    null;
  end some_proc;

-- run on package instantiation
begin
    init_time := systimestamp;
    -- just as a demo
    dbms_output.put_line('init_time is ' || init_time);
end p42;
/

当为每个会话实例化包时,即当该会话调用第一个过程或函数时,运行最后一个块;而在编译时,不是时,运行最后一个块:

That last block is run when the package is instantiated for each session, i.e. when the first procedure or function is called by that session, and not when it is compiled:

Package body P42 compiled

set serveroutput on
exec p42.some_proc;

PL/SQL procedure successfully completed.
init_time is 19-MAY-15 15.41.29.179387

...,而不是在相同会话中调用其他过程时:

... and not when other procedures are called from the same session:

exec p42.some_proc;

PL/SQL procedure successfully completed.

随后可以引用该变量.当然,拥有该变量意味着您现在具有程序包状态,这将导致其自身的问题.但是您可以只在初始化期间调用其他过程.

That variable could then be referenced later. Of course having that variable means you now have package state, which causes its own problems. But you could just call other procedures during initialisation instead.

在文档中阅读更多内容; 关于实例化

Read more in the documenation; about instantiation, state, and the initialisation part of the package body:

最后,主体可以具有初始化部分,该部分的语句初始化公共变量并执行其他一次性设置步骤.初始化部分仅在首次引用程序包时运行.初始化部分可以包括异常处理程序.

Finally, the body can have an initialization part, whose statements initialize public variables and do other one-time setup steps. The initialization part runs only the first time the package is referenced. The initialization part can include an exception handler.

这篇关于程序包启动时自动执行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆