《Oracle PL/SQL实例精讲》学习笔记2——PL/SQL概念

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hpdlzu80100/article/details/83591098

这一章学习到了以下内容:

1. PL/SQL架构(如,三层架构:数据管理层、应用处理层、表示层)

2. 替代变量的前缀有两种:&和&&,其中后者表示即使同名替代变量在代码中出现多次,用户仅需输入一次

问与答:

PL/SQL Architecture


a) Why it is more efficient to combine SQL statements into PL/SQL blocks?
Answer: It is more efficient to use SQL statements within PL/SQL blocks because
network traffic can be decreased significantly, and an application becomes more efficient
as well.
When an SQL statement is issued on the client computer, the request is made to the
database on the server, and the result set is sent back to the client. As a result, a single
SQL statement causes two trips on the network. If multiple SELECT statements are
issued, the network traffic can increase significantly very quickly. For example, four
SELECT statements cause eight network trips. If these statements are part of the PL/SQL
block, there are still only two network trips made, as in the case of a single SELECT
statement.
b) What are the differences between named and anonymous PL/SQL blocks?
Answer: Named PL/SQL blocks can be stored in the database and referenced later by
their names. Since anonymous PL/SQL blocks do not have names, they cannot be stored
in the database and referenced later.

How PL/SQL Gets Executed


a) What happens when an anonymous PL/SQL block is executed?
Answer: When an anonymous PL/SQL block is executed, the code is sent to the PL/SQL
engine on the server, where it is compiled.
b) What steps are included in the compilation process of a PL/SQL block?
Answer: The compilation process includes syntax checking, binding, and p-code
generation.

Syntax checking involves checking PL/SQL code for compilation errors. Once syntax
errors have been corrected, a storage address is assigned to the variables that are used to
hold data for Oracle. This process is called binding. Next, p-code is generated for the
PL/SQL block. P-code is a list of instructions to the PL/SQL engine. For named blocks,
p-code is stored in the database, and it is used the next time the program is executed.
c) What is a syntax error?
Answer: A syntax error occurs when a statement does not correspond to the syntax rules
of the programming language. An undefined variable or a misplaced keyword are
examples of syntax error.
d) How does a syntax error differ from a runtime error?
Answer: A syntax error can be detected by the PL/SQL compiler. A runtime error occurs
while the program is running and cannot be detected by the PL/SQL compiler.
A misspelled keyword is an example of the syntax error. For example, the script
BEIN
DBMS_OUTPUT.PUT_LINE ('This is a test');
END;
contains a syntax error. You should try to find this error.
A SELECT INTO statement returning no rows is an example of a runtime error. This
error can be handled with the help of the exception-handling section of the PL/SQL
block.

DBMS_OUTPUT.PUT_LINE Statement

a) What the DBMS_OUTPUT.PUT_LINE statement is used for?
Answer: The DBMS_OUTPUT.PUT_LINE statement is used to display information on
the screen.
b) Where does the DBMS_OUTPUT.PUT_LINE statement write its information prior to
displaying it on the screen?

Answer: First, the information is written into the buffer for storage. Once a program has
been completed, the information from the buffer is displayed on the screen. The size of
the buffer can be set between 2,000 and 1,000,000 bytes.
Substitution Variable Feature
a) What are substitution variables?
Answer: Substitution variables are special type of variables that enable PL/SQL to
accept input from a user at a run-time. Substitution variables cannot be used to output
values because no memory is allocated for them. Substitution variables are replaced with
the values provided by the user before the PL/SQL block is sent to the database.
b) How do you recognize substitution variable in a PL/SQL script?
Answer: Substitution variables are usually prefixed by the ampersand (&) character or
double ampersand (&&) character.
c) Why is it considered a good practice to enclose substitution variables with single quotes
for string datatypes?

Answer: A program cannot depend wholly on a user to provide text information in single
quotes. Enclosing a substitution variable with single quotes allows a program to be less
error-prone.
 

源代码如下:

-- *** Chapter Exercises *** --
-- For Example ch01_1a.sql
DECLARE
   v_first_name VARCHAR2(35);
   v_last_name  VARCHAR2(35);
BEGIN
   SELECT first_name, last_name
     INTO v_first_name, v_last_name
     FROM student
    WHERE student_id = 123;
   
   DBMS_OUTPUT.PUT_LINE ('Student name: '||v_first_name||' '||v_last_name);
EXCEPTION
   WHEN NO_DATA_FOUND 
   THEN
      DBMS_OUTPUT.PUT_LINE ('There is no student with student id 123');
END;

-- For Example ch01_1b.sql
DECLARE
   v_student_id NUMBER := &sv_student_id;
   v_first_name VARCHAR2(35);
   v_last_name  VARCHAR2(35);
BEGIN
   SELECT first_name, last_name
     INTO v_first_name, v_last_name
     FROM student
    WHERE student_id = v_student_id;
   
   DBMS_OUTPUT.PUT_LINE ('Student name: '||v_first_name||' '||v_last_name);
EXCEPTION
   WHEN NO_DATA_FOUND 
   THEN
      DBMS_OUTPUT.PUT_LINE ('There is no such student');
END;

-- For Example ch01_2a.sql
BEGIN
   DBMS_OUTPUT.PUT_LINE ('Today is '||'&sv_day');
   DBMS_OUTPUT.PUT_LINE ('Tomorrow will be '||'&sv_day');
END;

-- For Example ch01_2b.sql
BEGIN
   DBMS_OUTPUT.PUT_LINE ('Today is '||'&&sv_day');
   DBMS_OUTPUT.PUT_LINE ('Tomorrow will be '||'&sv_day'); 
END;

-- *** Web Chapter Exercises *** --
-- For Example ch01_3a.sql
DECLARE
   v_num    NUMBER := 10;
   v_result NUMBER;
BEGIN
   v_result := POWER(v_num, 2); 
END;

-- For Example ch01_3b.sql
DECLARE
   v_num    NUMBER := 10;
   v_result NUMBER;
BEGIN
   v_result := POWER(v_num, 2); 
   DBMS_OUTPUT.PUT_LINE ('The value of v_result is: '||v_result);
END;

-- For Example ch01_3c.sql
DECLARE
   v_num    NUMBER := &sv_num;
   v_result NUMBER;
BEGIN
   v_result := POWER(v_num, 2); 
   DBMS_OUTPUT.PUT_LINE ('The value of v_result is: '||v_result);
END;

-- For Example ch01_4a.sql
DECLARE
   v_radius NUMBER := &sv_radius;
   v_area   NUMBER;
BEGIN
   v_area := POWER(v_radius, 2) * 3.14; 
   DBMS_OUTPUT.PUT_LINE ('The area of the circle is: '||v_area);
END;

-- For Example ch01_5a.sql
DECLARE
   v_date DATE := SYSDATE;
BEGIN
   DBMS_OUTPUT.PUT_LINE ('Today is '||to_char(v_date, 'fmDay')||
                         ' at '     ||to_char(v_date, 'hh:mi AM'));
END;



猜你喜欢

转载自blog.csdn.net/hpdlzu80100/article/details/83591098