Updated on 2024-05-07 GMT+08:00

Anonymous Blocks

An anonymous block applies to a script infrequently executed or a one-off activity. An anonymous block is executed in a session and is not stored.

Syntax

Figure 1 shows the syntax diagrams for an anonymous block.

Figure 1 anonymous_block::=

Details about the syntax diagram are as follows:

  • The execution section of an anonymous block starts with a BEGIN statement, has a break with an END statement, and ends with a semicolon (;). Type a slash (/) and press Enter to execute the statement.

    The terminator "/" must be written in an independent row.

  • The declaration section includes the variable definition, type, and cursor definition.
  • A simplest anonymous block does not execute any commands. However, at least one statement, even a NULL statement, must be presented in any implementation blocks.

Examples

The following lists basic anonymous block programs:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
-- Null statement block
gaussdb=# BEGIN
     NULL; 
END;
/

-- Display information on the console.
gaussdb=# BEGIN
     dbe_output.print_line('hello world!'); 
END; 
/
hello world!
ANONYMOUS BLOCK EXECUTE
-- Display variable content on the console.
gaussdb=# DECLARE      
     my_var VARCHAR2(30);  
BEGIN      
     my_var :='world';     
     dbe_output.print_line('hello'||my_var); 
END; 
/ 
helloworld
ANONYMOUS BLOCK EXECUTE