Let you know software development in advance (31): begin and end in database scripts

Source: Internet
Author: User
Tags sybase database

Part 1 Database SQL language

Begin and end in database scripts

 

In database scripts, begin and end are a strange word. Without them, some code looks confusing; adding them makes the structure of the Code instantly clearer.

Indeed, begin and end are the start and end signs of code statements, which makes the logic of the script program clear and easy to read.

Begin and end are mainly used in the following places:

1. if, else, else if, while, and other statements

If, else, else if, while, and other statements must occupy one row, the execution statement should not be followed immediately, no matter how many statements are executed, the begin sign must be added... End.

The begin and end columns in the script file must have an exclusive row and are located in the same starting column. They are also left aligned with the statements that reference them. Begin... The code block within the end is indented. Generally, it is indented into four spaces.

Example:

If (@ varible1 <@ varible2)

Begin

[Statement execution]

End

 

Example 1:

If (@ varible1 <@ varible2)

[Execute Statement 1]

[Execute Statement 2]

In counterexample 1, due to the lack of begin and end, the "attribution problem" of "execution statement 1" and "execution statement 2" is raised with doubt, it is easy to confuse the logic of the Code.

 

Example 2:

If (@ varible1 <@ varible2)

Begin

[Statement execution]

End

In counterexample 2, begin and end are not in the same starting column, nor are they left aligned with the statements that reference them. In this way, the code looks messy.

 

Example 3:

If (@ varible1 <@ varible2) begin

[Statement execution]

End

 

In counterexample 3, The begin and if statements are in the same code line, which is also nonstandard.

 

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

When creating a stored procedure (such as a function or trigger), no matter how many rows are included in the execution statement content of the stored procedure (such as a function or trigger), it must start with begin and end, the name of the stored procedure (functions, triggers, etc.) is not added to the end.

Example 1 (create a stored procedure based on the 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 (30), -- name

@ Age int -- age

As

Declare @ begintime varchar (20), -- Start Time

@ Endtime varchar (20) -- End Time

Begin

[Statement execution part]

End

Go

 

Print 'create procedure pr_example OK'

Go

 

Example 2 (create 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 (20); -- Start Time

Endtime varchar2 (20); -- End Time

Begin

[Statement execution part]

End;

/

Prompt 'create procedure pr_example OK ';

 

In addition, when creating a stored procedure (function, trigger, etc.), each parameter must be in a separate row, and line breaks or multiple parameters in a row are not allowed. The comment of this parameter is either in the same line as this parameter, or occupies a single line. line feed is not allowed. The following code is nonstandard:

Example 3 (create 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 (20); -- Start Time

-- End Time

Endtime

Varchar2 (20 );

Begin

[Statement execution part]

End;

/

Prompt 'create procedure pr_example OK ';

 

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

 

 

 

(My microblogging: http://weibo.com/zhouzxi? Topnav = 1 & wvr = 5, No.: 245924426, welcome !)

Related Article

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.