Oracle SQL Plus The Definitive Guide- P7 ppsx

10 338 0
Oracle SQL Plus The Definitive Guide- P7 ppsx

Đang tải... (xem toàn văn)

Thông tin tài liệu

< previous page page_37 next page > Page 37 7 DBMS_OUTPUT_PUT_LINE(An error occurred.); 8 END; 9 / PL/SQL procedure successfully completed. Where's the output? Now you may be wondering why there was no output from the code block in the previous section. After all, the code does contain a call to the PUT_LINE procedure that sure looks as if it ought to display something. In fact, the code did generate some output. You just didn't see it. Remember from Chapter 1 that SQL*Plus itself does not execute PL/SQL code. It just sends that code to the database server, which executes the code for you. The Oracle database server doesn't have any way to directly display the output for you to see. Instead, any output from PL/SQL code is buffered by the server for later retrieval by the application that executed it, in this case SQL*Plus. By default, SQL*Plus does not retrieve PL/SQL output from the server. You have to tell it to retrieve the output if you want to see it. The command for that is: SET SERVEROUTPUT ON If you enter the above command, followed by the same PL/SQL block that you entered earlier, your output will look like this: SQL> SET SERVEROUTPUT ON SQL> DECLARE 2 X VARCHAR2(12) := Hello World!; 3 BEGIN 4 DBMS_OUTPUT.PUT_LINE(X); 5 EXCEPTION 6 WHEN OTHERS THEN 7 DBMS_OUTPUT.PUT_LINE(An error occurred.); 8 END; 9 / Hello World! PL/SQL procedure successfully completed. This time around, you do see the output from the block. The SERVEROUTPUT setting sticks for the duration of your SQL*Plus session, so you don't have to keep turning it on each time you execute another block. There are some other parameters to the SET SERVEROUTPUT command that affect formatting and the output buffer size. The SIZE parameter lets you increase the buffer size from the default of 2000 bytes, something you should do if you expect to display a lot of information from PL/SQL. The FORMAT parameter lets you control whether, and how, long lines of output are wrapped when they are displayed. The following exam- < previous page page_37 next page > < previous page page_38 next page > Page 38 ple shows how you can turn server output on, allow for a maximum of 1,000,000 bytes to be displayed, and word-wrap any long lines. SET SERVEROUTPUT ON SIZE 1000000 FORMAT WORD_WRAPPED Prior to version 8 of SQL*Plus, the SIZE and FORMAT parameters did not exist. To increase the buffer size, you had to make a call to DBMS_OUTPUT.ENABLE. You can read about this in Chapter 11, Customizing Your SQL*Plus Environment, under the section titled The SET Command. Rules for entering PL/SQL blocks When you begin entering a PL/SQL block, SQL*Plus switches to what is called PL/SQL mode. It knows to do this by watching for the keywords BEGIN and DECLARE, either of which may start a PL/SQL block. Once in PL/SQL mode, you can pretty much type anything you please. SQL*Plus simply buffers everything you type until you terminate PL/ SQL mode by typing one of the termination characterseither a forward slash or a period on a line by itself. Parsing and syntax checking of your PL/SQL code is done by the database server, not by SQL*Plus, and doesn't happen until after you have completely entered and terminated the block. The following SQL commands also put you into PL/SQL mode: CREATE PROCEDURE, CREATE FUNCTION, CREATE TRIGGER, CREATE PACKAGE, CREATE PACKAGE BODY, and CREATE TYPE. That's because these commands allow you to define stored objects based on PL/ SQL code. The rules for entering a PL/SQL block are as follows: The first word of a PL/SQL block must be BEGIN, DECLARE, CREATE PROCEDURE, CREATE FUNCTION, CREATE TRIGGER, CREATE PACKAGE, or CREATE TYPE. Lowercase is OK; PL/SQL is not case-sensitive. PL/SQL blocks may span multiple lines. Line breaks may occur anywhere you can legally enter whitespace. Comments, delimited by /* */, may be embedded anywhere whitespace is allowed. These commands may span multiple lines. A double hyphen ( ) makes everything after it on the same line a comment. Blank lines are allowed in a PL/SQL block, but SQL*Plus filters them out. < previous page page_38 next page > < previous page page_39 next page > Page 39 Entry of a PL/SQL block must be terminated in one of two ways: By use of the forward slash character. The forward slash must be on a line by itself, and must be in column 1 of that line. Using a forward slash tells SQL*Plus to execute the block you have just entered. By use of a period. The period must be on a line by itself, and in the first position. Using a period causes the statement to be stored in the SQL buffer rather than being executed immediately. Because blank lines are allowed within a block of code, they can't be used to terminate a block. That's where the period comes into play. Oracle needed to provide a way to enter a PL/SQL block into the buffer without executing it. Since a blank line can't be used for that purpose, as it can be with a SQL statement, Oracle decided to allow the period on a line by itself to serve this function. Likewise, because a PL/SQL block may be made up of many statements, each of which itself ends with a semicolon, that character cannot reliably be used as a termination character. So to enter and execute a block, we are left with only the forward slash. Executing a Single PL/SQL Command The SQL*Plus EXECUTE command may be used to execute a singe PL/SQL statement. The syntax is simply: EXECUTE statement where: EXEC[UTE] May be abbreviated to EXEC. statement Is the PL/SQL statement you want to execute. EXECUTE is most helpful when you want to make a quick call to a PL/SQL function. In the following example, EXECUTE is used to make a call to DBMS_OUTPUT.ENABLE, in order to allow more than the default 2000 bytes of PL/SQL output to be displayed. SQL> EXECUTE DBMS_OUTPUT.ENABLE(10000) The value of 10,000 in this example tells Oracle to allow for up to 10,000 bytes of output to be displayed by the DBMS_OUTUT.PUT_LINE command procedure. The EXECUTE command is nothing more than a SQL*Plus shortcut. SQL*Plus takes whatever text you type after EXECUTE, adds a semicolon to the end, wraps the keywords BEGIN and END around it, and sends it to Oracle as just another PL/SQL block. < previous page page_39 next page > < previous page page_40 next page > Page 40 The SQL Buffer SQL*Plus keeps a copy of the most recently entered SQL statement or PL/SQL block in an internal memory area known as the SQL buffer, often referred to simply as the buffer. The reason for this is simple. SQL*Plus needs a place to store your statement or block until you are finished entering it. SQLlus also provides you with the ability to edit the statement in the buffer. This can be a real convenience if you make a mistake halfway through typing a long, multiline query. SQL*Plus buffers SQL statements and PL/SQL blocks, but not SQL*Plus commands. For example, the DESCRIBE command would not be buffered, but a SELECT statement would be. To help make the distinction, it may help to think in terms of where the command is executed. If you enter something to be executed by the database server, then it is buffered. If it's a command local to SQL*Plus, then it is not buffered. SQL*Plus provides two ways to edit the statement currently in the buffer. The first method is to use the set of line- editing commands built into SQL*Plus. The second method is to use the EDIT command to invoke an operating system- specific text editor, such as Notepad in the Windows environment, or vi under Unix. If you are editing a statement in the buffer, be sure you don't forget yourself and execute any other SQL command. Even a simple SQL command like COMMIT will overwrite the buffer. Commands to SQL*Plus, such as the editing commands, do not affect the buffer. There are some other useful things you can do because of the buffer. If you have several similar SQL statements to execute, using the buffer can save you the effort of completely typing out each one. You may be able to enter the first statement, execute it, make a minor change, execute the new statement, and so on until you are done. SQL*Plus also allows you to save and load the buffer to and from a text file, allowing you to store frequently executed statements for later use. Line Editing The concept of line editing goes way back to the days when all many people had to work with were dumb terminals that didn't allow full-screen editing, and connection speeds were so slow that full-screen editing would have been very painful anyway. A good line editor will allow you to work productively at speeds as < previous page page_40 next page > < previous page page_41 next page > Page 41 low as 300 bits per second. While this isn't much of a concern today, it accurately reflects the environment at the time SQL*Plus was first conceived. The line-editing process in SQL*Plus follows these steps: 1. You enter a SQL statement or PL/SQL block, which SQL*Plus stores in the buffer. 2. You then list the contents of the buffer to the screen. 3. You enter SQL*Plus commands that tell SQL*Plus to make changes to the statement in the buffer. 4. You list the buffer again. 5. If you like what you see, you execute the statement; otherwise, you go back to step three and make some more changes. I can remember that in my younger days my fellow programmers and I always took a great pride in the number of line- editing changes we could make and visualize in our heads before we had to break down and list our code again. The Current Line When working with the line editor in SQL*Plus, it's important to understand the concept of the current line. Simply put, the current line is the one that you have most recently touched. When you are entering a statement, the most recently entered line is the current line. Consider the following SQL statement for example: SQL> SELECT employee_name, project_name 2 FROM employee, project, project_hours 3 WHERE employee.employee_id = project_hours.employee_id 4 AND project_hours.project_id = project.project_id 5 GROUP BY employee_name, project_name 6 SQL> The statement shown above is five lines long. Line 6 doesn't count, and is not added to the buffer, because that's where the blank line was used to terminate entry of the statement. In this case, the last line touched happens to be the last line entered, so line 5 is the current line. Most line-editing commands, by default, operate on the current line. Some commands, such as LIST and DEL, allow you to specify a line number. When you specify a line number for a command, the command is executed, and that line then becomes the new current line. You'll see how this works as you read through the examples that follow. < previous page page_41 next page > < previous page page_42 next page > Page 42 Line-Editing Commands SQL*Plus contains a number of useful line-editing commands, some of which have several variations. Most of these commands may be abbreviated to one letter. Table 2-1 describes each of these commands, and shows the abbreviations and variations for each one. Table 2-1. SQL*Plus Line-Editing Commands Command Abbreviation Variations Description APPEND A A text Appends text to the end of the current line. CHANGE C C /from/to/ Scans the current line for the string from, and replaces the first occurrence of from with to. C /delete/ Deletes the first occurrence of delete from the current line. Think of this as changing delete to an empty string. DEL none DEL Deletes the current line. DEL linenum Deletes line number linenum from the buffer. DEL start end Deletes lines start through end from the buffer. INPUT I I Allows you to add one or more lines of text to the buffer. These lines are inserted into the buffer immediately following the current line. I text Adds just one line to the buffer, consisting of text, which is inserted immediately following the current line. LIST L L Displays the entire buffer on the screen for you to see. L linenum Lists a specific line number, and makes that line current. L start end Displays the specified range of lines, making the last line of that range current. linenum Lists that line number, making the line current. CLEAR BUFFER CL BUFF CL BUFF Clears the buffer. This deletes all the lines in one shot. Notice that two of the commands, LIST and DEL, allow you to specify a line number or a range of line numbers. For these two commands, there are two special < previous page page_42 next page > < previous page page_43 next page > Page 43 keywords you can use in place of a number. These keywords are * and LAST, and have the meanings shown here: * An asterisk always refers to the current line. LAST The keyword LAST refers to the last line in the buffer. You will see examples of how these elements are used as you read more about each of the commands. Getting a statement into the buffer To put a SQL statement into the buffer, simply enter the statement and terminate it with a blank line, as shown here: SQL> SELECT * 2 FROM project 3 The statement is actually inserted into the buffer one line at a time as you enter it. Pressing ENTER on a blank line tells SQL*Plus to leave it there without transmitting it to the server. PL/SQL blocks are entered the same way, except that you terminate them by entering a period on the last line. Following is an example of the shortest PL/SQL block one can write: SQL> BEGIN 2 NULL; 3 END; 4 . Terminating the block with a period tells SQL*Plus not to send it to the database, but just to keep it in the buffer. LIST The LIST command shows you the current contents of the buffer. It is fundamental to the use of the other line-editing commands. Use LIST to view your SQL statement as it currently exists, to see if any changes need to be made. Use LIST after making changes to be sure that they were made correctly. Look at the following example, which shows a SQL statement being entered into SQL*Plus, and then shows the LIST command being used to display it again: SQL> SELECT employee_name, time_log_name, project_name 2 FROM employee, project 3 WHERE employee. employee_num = time_log.employee_num 4 HAVING employee_name = project_name 5 AND time_log.project_num = project.project_num 6 GROUP BY employee_name, project_name 7 SQL> LIST < previous page page_43 next page > < previous page page_44 next page > Page 44 1 SELECT employee_name, time_log_name, project_name 2 FROM employee, project 3 WHERE employee. employee_num = time_log.employee_num 4 HAVING employee_name = project_name 5 AND time_log.project_num = project.project_num 6* GROUP BY employee_name, project_name Notice the asterisk marking line number 6. The asterisk indicates the current line, which LIST always sets to be the last line displayed. You can display just the current line by using LIST *, as in the following example: SQL> LIST * 6* GROUP BY employee_name, project_name You can display one specific line by specifying the line number as an argument to the LIST command. The next example shows how to list line 3: SQL> LIST 3 3* WHERE employee.employee_num = time_log.employee_num Notice the asterisk. By listing line 3 you have made it the current line for editing purposes. The keyword LAST may be used to display the last line in the buffer. For example: SQL> LIST LAST 6* GROUP BY employee_name, project_name You may also specify a range of lines to be displayed. This is done by specifying both the starting and ending lines as arguments to the LIST command. Either or both of these arguments may be the keyword LAST or *. The next example shows several different ways to display a range of lines using LIST. SQL> LIST 1 3 List lines 1 through 3 1 SELECT employee_name, time_log_name, project_name 2 FROM employee, project 3* WHERE employee.employee_num = time_log. employee_num Line 3 is now current SQL> LIST * LAST List everything beginning from the currentline 3 WHERE employee.employee_num = time_log.employee_num 4 HAVING employee_name = project_name 5 AND time_log.project_num = project.project_num 6* GROUP BY employee_name, project_name Line 6 is now current SQL> LIST 4* List from line 4 through 6 (the current line) 6* GROUP BY employee_name, project_name SQL> LIST * * A one-line range, same effect as LIST* 6* GROUP By employee_name, project_name SQL> LIST LAST LAST A one_line range, same as LIST LAST 6* GROUP BY employee_name, project_name As a shortcut to using the LIST command, if there is just one line you are interested in, you can list it by entering the line number itself and then pressing < previous page page_44 next page > < previous page page_45 next page > Page 45 ENTER. This won't work for a range of lines, but it will work for just one. Here's an example: SQL> 3 3* WHERE employee.employee_num = time_log.employee_num On a six-line statement, you might wonder why you would ever bother to list just one line or a range of lines. Remember, though, that line speeds were very slow when SQL*Plus was first developed. In addition, SQL statements and PL/SQL blocks are often much longer than six lines. Listing a range allows you to focus on one area at a time while you fix it. Keep the above SQL statement in the buffer as you read on about the rest of the line-editing commands. It has several mistakes that will be fixed using the other commands. APPEND The APPEND command is used to add text onto the end of a line. It works on the current line, so you must first decide which line you want to change and then make that line current. Use the LIST command to review the SQL statement currently in the buffer: SQL> L 1 SELECT employee_name, time_log_name, project_name 2 FROM employee, project 3 WHERE employee. employee_id = time_log.employee_id 4 HAVING employee_name = project_name 5 AND time_log.project_id = project.project_id 6* GROUP BY employee_name project_name I really intended this SELECT statement to join all three sample tables, but if you look at line 2, you will see that I forgot to include the PROJECT_HOURS table. This can easily be corrected by first making line 2 the current line, and then using the APPEND command to add the third table to the join. The first step is to LIST line 2 in order to make it current: SQL> L 2 2* FROM employee, project Now that line 2 is the current line, the APPEND command may be used to add PROJECT_HOURS to the join: SQL> A , project_hours 2* FROM employee, project, project_hours Notice that APPEND took all the text following the command, appended it to the current line, then redisplayed that line to show the results of the edit. Now the SELECT statement in the buffer joins all three tables. < previous page page_45 next page > < previous page page_46 next page > Page 46 CHANGE The CHANGE command searches the current line for a specified string and replaces that string with another. CHANGE only replaces the first occurrence it finds, so if you need to change multiple occurrences of a string in the same line, you will need to execute the same CHANGE command several times. CHANGE may also be used to simply delete text from a line. List the contents of the buffer again. Your output should match that shown below: SQL> L 1 SELECT employee_name, time_log_name, project_name 2 FROM employee, project, project_hours 3 WHERE employee.employee_num = time_log.employee_num 4 HAVING employee_name = project_name 5 AND time_log.project_num = project.project_num 6* GROUP BY employee_name, project_name Notice that line 1 references a column that does not exist. In just a bit you will see how to remove that column reference with the CHANGE command. Next, notice that the WHERE clause has three mistakes: the table name TIME_LOG is used instead of PROJECT_HOURS, and employee_num is used twice when it really should be employee_id. The CHANGE command can be used to fix all these problems. To start with, here's how to change TIME_LOG to PROJECT_HOURS: SQL> L 3 3* WHERE employee.employee_num = time_log.employee_num SQL> C /time_log/project_hours/ 3* WHERE employee.employee_num = project_hours.employee_num In the above example, the LIST command is first used to make line 3 the current line. Then the CHANGE command, abbreviated to C, is used to change the table name. After the edit is complete, the line is automatically redisplayed so you can see the effects of the change. Next, the employee_num field name needs to be corrected. It should be employee_id. Remember, CHANGE only does one substitution at a time. Since there are two occurrences of employee_num that need to be changed, CHANGE will have to be executed twice. The following example shows this: SQL> L 3 3* WHERE employee.employee_num = project_hours.employee_num SQL> C /employee_num/employee_id/ 3* WHERE employee.employee_id = project_hours.employee_num SQL> C /employee_num/employee_id/ 3* WHERE employee.employee_id = project_hours.employee_id Notice that the CHANGE command searched the current line from left to right. The leftmost occurrence of employee_num was the first to be changed. Notice also that < previous page page_46 next page > . reflects the environment at the time SQL* Plus was first conceived. The line-editing process in SQL* Plus follows these steps: 1. You enter a SQL statement or PL /SQL block, which SQL* Plus stores in the. buffer. 2. You then list the contents of the buffer to the screen. 3. You enter SQL* Plus commands that tell SQL* Plus to make changes to the statement in the buffer. 4. You list the buffer again. 5 overwrite the buffer. Commands to SQL* Plus, such as the editing commands, do not affect the buffer. There are some other useful things you can do because of the buffer. If you have several similar SQL

Ngày đăng: 05/07/2014, 04:20

Mục lục

  • Локальный диск

    • cover

    • page_iii

    • page_iv

    • page_v

    • page_vii

    • page_viii

    • page_ix

    • page_xi

    • page_xii

    • page_xiii

    • page_xiv

    • page_xv

    • page_xvi

    • page_xvii

    • page_xviii

    • page_xix

    • page_xx

    • page_xxi

    • page_1

    • page_2

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan