简体   繁体   English

pl / sql中的过程中的多个/嵌套的开始/结束,函数语句

[英]multiple/nested begin/end, function statements in procedures in pl/sql

i´m new to PL/SQL and don´t know how to write it clean and with good practice. 我是PL / SQL的新手,并且不知道如何使用良好的实践将它编写得井井有条。 I read and watched a view tutorials but these examples were much easyier than my procedure. 我阅读并观看了视图教程,但是这些示例比我的过程要容易得多。 To create a "large" readable, clean procedure i would use multiple and/or nested begin/ends and functions like this: 为了创建一个“大的”可读,干净的过程,我将使用多个和/或嵌套的开始/结束和函数,如下所示:

begin statement... function... end function exception end

begin.... and so on

but i´m not sure if this is a lean way to write PL/SQL. 但是我不确定这是否是编写PL / SQL的精简方法。 I need multiple selects on different tables, find rows use them and isert and update rows in different tables. 我需要在不同的表上进行多次选择,查找使用它们的行,然后对不同表中的行进行插入和更新。 All that in a single procedure. 所有这些只需一个过程。 In general i would use begin/end for new Statements, declares for just locally used variables and functions for operations on the result of a statement. 通常,我将对新的Statement使用begin / end,仅对本地使用的变量和函数的声明进行声明的结果的操作。 Would this be good practice? 这是个好习惯吗?

Thanks for your help 谢谢你的帮助

begin...end; alone does nothing. 一个人什么也没做。 You only need a nested block if it has a declare or exception that makes sense at that level. 仅当嵌套块具有在该级别有意义的declareexception ,才需要嵌套块。

For example, the inner begin / end keywords here are pointless, and you can just remove them: 例如,此处的内部begin / end关键字毫无意义,您可以删除它们:

begin
    ...

    begin
        processing steps
    end;

    ...
end;

A nested block is useful if we want to handle some exception in a nested block only (for example, handle no_data_found exceptions for a particular lookup, or file operations for utl_file ). 如果我们只想处理嵌套块中的某些异常(例如,为特定查找处理no_data_found异常,或对utl_file文件操作),则嵌套块很有用。 This is often better than having exception handlers at the end, especially for procedures with a lot of processing steps: 这通常比在末尾具有异常处理程序更好,尤其是对于具有许多处理步骤的过程而言:

begin
    ...

    begin
        processing steps
    exception
        when no_data_found then...
    end;

    ...
end;

Similarly, we might declare a local variable or constant etc with the scope of the nested block only. 同样,我们可以声明一个局部变量或常量等,其范围仅是嵌套块的范围。 This is probably a less usual requirement than the exception example, but there are cases where it could be useful: 与例外示例相比,这可能是一个不太常见的要求,但是在某些情况下它可能是有用的:

begin
    ...

    declare
        x constant integer := whatever();
    begin
        processing steps using x
    end;

    ...
end;

If the procedure is complex with a large number of steps, you might consider ways to break it up into smaller reusable modules. 如果该过程很复杂且包含大量步骤,则可以考虑将其分解为较小的可重用模块的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM