Understanding software Development in advance (31) Begin and end in database scripts

Source: Internet
Author: User
Tags execution sybase database oracle database

In database scripts, begin and end are a pair of strange words. Without them, some code looks confused, adding them, and the structure of the code is instantly clear.

Indeed, beginning and end are the starting and ending flags for code statements, making the scripting logic clear and easy to read.

Begin and end are mainly used in the following places:

1. If, else, else if, while, etc statements

If, else, else if, while and other statements to own a row, the execution statement must not immediately follow, no matter how many execution statements have to add the statement block flag begin...end.

The Begin and end in the script file should be exclusive and in the same starting column, aligned to the left of the statement that references them. The code block within the Begin...end uses indents, typically indented to 4 spaces.

Positive example:

if (@varible1 < @varible2)

Begin

[EXECUTE statement]

End

Counter Example 1:

if (@varible1 < @varible2)

[EXECUTE statement 1]

[EXECUTE Statement 2]

In inverse 1, because of the lack of begin and end, the "Attribution question" of "EXECUTE statement 1" and "EXECUTE Statement 2" is confusing, and it is easy to make the logic of the code wrong.

Counter Example 2:

if (@varible1 < @varible2)

Begin

[EXECUTE statement]

End

In anti-Example 2, begin and end are not in the same starting column, nor are left-aligned with the statement that references them. So the code looks messy.

Counter Example 3:

if (@varible1 < @varible2) begin

[EXECUTE statement]

End

In anti-Example 3, it is also not normal for the begin and if statements to be in the same line of code.

2. When creating stored procedures (functions, triggers, etc.)

When a stored procedure (function, trigger, etc.) is created, the number of rows in the execution statement content portion of the stored procedure (function, trigger, and so on) must begin with begin, end, and the name of the stored procedure (function, trigger, and so on).

Example 1 (creating stored procedures based on Sybase database):

if exists (select 1 from sysobjects where name = ' pr_example ')
    
begin
    
    drop procedure pr_example
    
end
    
go
  
   create procedure pr_example
    
    @name      varchar ()-         -name
    
    @age        int                --age
    
as DECLARE @begintime          varchar,  --Start time
    
      @endtime            varchar ()--  end time
    
begin
    
    [ Execute statement part] End go
    
     
    
print ' CREATE PROCEDURE pr_example OK '
    
go
  

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/project/

Example 2 (creating a stored procedure based on an Oracle database):

Create or Replace procedure Pr_example
    
(
    
    v_name  in Varchar2,    --name
    
    v_age          out  int         --age
    
)
    
As
    
    begintime     varchar2 (a);      --Start time
    
    endtime      varchar2;      End time
    
begin
    
    [EXECUTE statement part]
    
;
    
/
    
Prompt ' CREATE procedure pr_example OK ';

In addition, when you create stored procedures (functions, triggers, and so on), each parameter must be on a single line, with no line wrapping or multiple arguments allowed. The comment for this parameter is either on the same line as the parameter or on a separate line, and does not allow wrapping on the parameter line. The following code is not canonical:

Example 3 (creating a stored procedure based on an Oracle database):

Create or Replace procedure Pr_example
    
(
    
    v_name  in Varchar2,    --name
    
    v_age          out  int
    
--age
    
)
    
As
    
    begintime     varchar2 (a);      --Start time-
    
    -end time
    
    endtime     
    
varchar2;
    
Begin
    
    [EXECUTE statement part] End
    
;
    
/
    
Prompt ' CREATE procedure pr_example OK ';

In the actual software project, the proper use of begin and end can make the logic of the code clear and readable. This is conducive to the improvement of work efficiency.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.