Tài liệu Oracle SQL Jumpstart with Examples- P4 docx

50 315 0
Tài liệu Oracle SQL Jumpstart with Examples- P4 docx

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

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

120 6.3 Sorting Methods BEGIN vSPLIT := INSTR(pTIME,':'); vHOURS := TO_NUMBER(SUBSTR(pTIME,1,vSPLIT-1)); vSECONDS := TO_NUMBER(SUBSTR(pTIME,vSPLIT+1)); RETURN vHOURS+(vSECONDS/60); EXCEPTION WHEN OTHERS THEN RETURN 0; END; / And now we can replace the ORDER BY clause with the function as an expression, making for a much easier to read ORDER BY clause. The result is shown in Figure 6.9. ORDER BY GETTIME(PLAYING_TIME) NULLS FIRST, 3 DESC, 1; Figure 6.9 Using an Expression in the ORDER BY Clause. Chap6.fm Page 120 Thursday, July 29, 2004 10:04 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 6.3 Sorting Methods 121 Chapter 6 Obviously, a position number cannot be applied to the expression unless the expression is placed into the SELECT list. Note: Copying the expression from the ORDER BY into the SELECT col- umns list could possibly help performance. We can change the query something like that shown following. The expression GETTIME(PLAYING_TIME) has been added to the query, and the ORDER BY clause has been changed to accommodate it. The result in Figure 6.10 shows the same sorted order on the PLAYING_TIME column value as shown in Figure 6.9. SELECT RECORDING_DATE, PLAYING_TIME, GETTIME(PLAYING_TIME) , TITLE FROM SONG WHERE TITLE LIKE '%a%' AND TITLE LIKE '%e%' AND TITLE LIKE '%i%' ORDER BY 3 NULLS FIRST, 4 DESC, 1; Figure 6.10 ORDER BY Clause Expressions Cannot Use Positions. Chap6.fm Page 121 Thursday, July 29, 2004 10:04 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 122 6.4 Endnotes You have now added most of the fundamental features to the SELECT statement, namely the SELECT, FROM, WHERE, and ORDER BY clauses. Later chapters expand on a multitude of other features and mecha- nisms. The next chapter digresses somewhat and covers operators, condi- tions, and pseudocolumns. 6.4 Endnotes 1. Oracle Performance Tuning for 9i and 10g (ISBN: 1-55558-305-9) Chap6.fm Page 122 Thursday, July 29, 2004 10:04 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 123 7 Operators, Conditions, and Pseudocolumns In this chapter:  What is precedence?  What is an operator and what is available?  What is a condition and what is available?  What are pseudocolumns and what is available? Note: Backus-Naur Form syntax angle brackets are used syntactically in this chapter to represent substitution of all types. For example, <expression> = <expression>. Operators, conditions, and pseudocolumns are used and often explained throughout this book. This chapter may duplicate parts of other chapters with the intention of including all specific details in a single chapter. Addi- tionally, some of the content of this chapter is common to many software products. Need it be repeated? Yes, because this title is intended for use as an SQL reference book. Note: This chapter may reclassify the categories of operators, conditions, and pseudocolumns both with respect to Oracle documentation and other chapters in this book. Let’s begin with the simplest of things, precedence. Chap7.fm Page 123 Thursday, July 29, 2004 10:04 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 124 7.2 Operators 7.1 Precedence One factor important with regards to both operators and conditions is that of precedence. Precedence implies that one operator is executed before another. Enclosing part of an expression in brackets (parentheses in mathe- matical jargon) forces that part of the expression to be executed first, start- ing with the lowest nested or parenthesized level. Let’s look at arithmetic operator precedence to explain this concept. In this first example expression, the multiplication will execute before the addition because multiplication has higher precedence than (is executed before) addition, even though reading from left to right, addition appears before multiplication. x + y × z Now let’s fix the precedence problem and force addition to execute first by using parentheses. ( x + y ) × z Similarly applying nesting of precedence in the next example, the sub- traction will be executed first, followed by the addition and finally the mul- tiplication, regardless of the precedence of the different operators. ( x + ( y - p )) × z That is precedence. Simple, right? Now let’s go onto operators. 7.2 Operators Operators can be divided into several groups, as shown following:  Arithmetic operators allow things like 1 + 1 or 5 * 3, where + and * are the arithmetic operators.  Logical operators allow merging of multiple expressions.  The concatenation operator is the || goodie allowing concatenation of strings.  Hierarchical query operators are specialized for use in hierarchical queries.  Set operators literally do things with sets of rows.  Multiset operators are set operators exclusively for use with nested table objects.  User-defined operators allow creation of your own operators. Chap7.fm Page 124 Thursday, July 29, 2004 10:04 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 7.2 Operators 125 Chapter 7 7.2.1 Arithmetic Operators An arithmetic operator allows for simple arithmetic calculations in the form shown: <expression> <operator> <expression>  * and / execute multiplication and division, respectively, both having the same precedence and both having higher precedence than addi- tion and subtraction.  + and – execute addition and subtraction, respectively, both having the same precedence. This example shows use of an arithmetic operator in a SELECT state- ment producing the “Owed” column. The result is shown in Figure 7.1 SELECT ARTIST_ID, SESSION_DATE, AMOUNT_CHARGED, AMOUNT_PAID , AMOUNT_CHARGED - AMOUNT_PAID “Owed” FROM STUDIOTIME; Figure 7.1 Arithmetic Operators. Chap7.fm Page 125 Thursday, July 29, 2004 10:04 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 126 7.2 Operators 7.2.2 Logical Operators Logical operators are NOT, AND, and OR, in that order of precedence. NOT implies that an expression must be false for a TRUE result; AND implies that two expressions must be true for a TRUE result; and OR implies that either of two expressions must be true for a TRUE result. There are more examples in Chapter 5.  <expression> AND <expression> such that both expressions yield TRUE. This example finds artists whose names contain the vowel “a” and who live in the USA. Both conditions must be true for a row to be returned. The result is shown in Figure 7.2. SELECT NAME, COUNTRY FROM ARTIST WHERE COUNTRY = 'USA' AND NAME LIKE '%a%'; Figure 7.2 The AND Logical Operator. Chap7.fm Page 126 Thursday, July 29, 2004 10:04 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 7.2 Operators 127 Chapter 7  <expression> OR <expression> such that either expression yields TRUE. This example is the same as the last except that either expres- sion can be true. The result in Figure 7.3 shows any artists either in the USA or with the vowel “a” in their names. SELECT NAME, COUNTRY FROM ARTIST WHERE COUNTRY = 'USA' OR NAME LIKE '%a%';  <expression> { AND | OR } NOT <expression> yields TRUE if both expressions (AND), or either (OR), yield TRUE. Figure 7.4 shows artists in the USA as long as the vowel “a” is not in their names. SELECT NAME, COUNTRY FROM ARTIST WHERE COUNTRY = 'USA' AND NOT NAME LIKE '%a%'; Figure 7.3 The OR Logical Operator. Chap7.fm Page 127 Thursday, July 29, 2004 10:04 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 128 7.2 Operators 7.2.3 The Concatenation Operator The concatenation operator (||) allows concatenation of strings. The exam- ple following concatenates two strings from two separate tables in an SQL join (see Chapter 10). The result is shown in Figure 7.5. SELECT NAME||' WROTE '||TITLE FROM ARTIST NATURAL JOIN SONG WHERE TITLE LIKE '%A%'; 7.2.4 Hierarchical Query Operators There are two hierarchical query operators, which are discusssed in more detail with examples in Chapter 13.  PRIOR is used with the CONNECT BY condition evaluating the subsequent expression for each parent row of each current row, using a current row column to hook into a parent row column.  CONNECT_BY_ROOT performs a similar function to that of CONNECT BY PRIOR except using the root row of the hierar- chy as opposed to the parent row. Figure 7.4 The NOT Logical Operator. Chap7.fm Page 128 Thursday, July 29, 2004 10:04 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 7.2 Operators 129 Chapter 7 7.2.5 Set Operators The various set operators effectively allow the merging of results of two sep- arate queries in the form of <query> operator <query> (more detail and examples in Chapter 13).  UNION [ ALL ] retrieves all rows in both queries. The ALL modifier includes all duplicates; otherwise only unique rows are retrieved.  INTERSECT returns the intersection of two queries, namely rows common to both queries.  MINUS returns all unique rows in the first query but not in the sec- ond query. 7.2.6 Multiset Operators Where set operators do things with query results of two queries, multiset operators perform a similar function between two nested tables. Require- Figure 7.5 The Concatenation (||) Operator. Chap7.fm Page 129 Thursday, July 29, 2004 10:04 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... the B .SQL and C .SQL calls to be executed with full path names The A .SQL script looks as follows: SET SERVEROUTPUT ON; EXEC DBMS_OUTPUT.PUT_LINE('Executing A .SQL' ); @C:\TEMP\B .SQL; EXEC DBMS_OUTPUT.PUT_LINE ('Completed A .SQL' ); SET SERVEROUTPUT OFF; The script B .SQL looks as follows: EXEC DBMS_OUTPUT.PUT_LINE('Executing B .SQL' ); @@C .SQL; EXEC DBMS_OUTPUT.PUT_LINE ('Completed B .SQL' ); The script C .SQL. .. imagine you have three scripts named A .SQL, B .SQL, and C .SQL in the C:\TEMP directory /tmp on UNIX The script A .SQL has an SQL* Plus command to run the B .SQL and B .SQL calls C .SQL You could start the primary script by typing this command The other two scripts will be found because they are stored in the same directory @@C:\TEMP\A The double @@ command simply tells SQL* Plus to use the current directory... settings for SQL* Plus formatting? How are variables used in SQL* Plus? How are scripts used in SQL* Plus? How are reports formatted in SQL* Plus? How is iSQL*Plus used for reporting? This chapter shows you how to use the environmental settings, variables, and special SQL* Plus commands to generate acceptable output and reports Examples in this book use both SQL* Plus Worksheet and SQL* Plus SQL* Plus Worksheet... excess of 70 different environmental variables you can set Most environmental variables are SQL* Plus variables you adjust for your Oracle Database 10g session and can only be used with SQL* Plus tools (i.e., SQL* Plus, SQL* Plus Worksheet, and iSQL*Plus) Look at the entire list by running the following statement in SQL* Plus Worksheet: SHOW ALL Figure 8.1 shows part of the results Figure 8.1 Environmental... command to write the SQL buffer contents to a file so you can edit and run it whenever you want This also allows you to add SQL* Plus commands to the file, which do not get saved when working only with the SQL buffer file SAVE CDREPORT The file is named CDREPORT .SQL The SQL extension is a default If you specify the extension (suffix) in the SAVE command file name, the default SQL is overridden with the specified... editor returns the query to SQL* Plus A forward slash will execute the query SQL* Plus prompts you to enter a value for the variable Figure 8.11 shows how this looks Figure 8.11 SQL* Plus Can Prompt for Variable Values Typing the number 1 and pressing Enter forces SQL* Plus to replace the variable with a number 1 and execute the query Figure 8.12 shows the result Figure 8.12 SQL* Plus Variable Substitution... covers more detail using SQL* Plus, particularly with respect to formatting Further SQL* Plus output formatting detail is essential to proper use and understanding of Oracle SQL Chapter 7 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark This page intentionally left blank Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 8 Using SQL* Plus In this chapter:... EXEC DBMS_OUTPUT.DISABLE; SET SERVEROUTPUT OFF; Figure 8.5 shows the result of the previous script with the buffer (DBMS_OUTPUT.DISABLE) set to its default of 2,000 characters SQLP[ROMPT] This option changes the SQL prompt Use the command SET SQLPROMPT ' ' to remove the prompt altogether Use SET SQLPPROMPT 'SQL> ' to return to the default The example as shown following sets an interesting prompt, resulting... DBMS_OUTPUT.PUT_LINE('Executing A .SQL' ); @@B .SQL; EXEC DBMS_OUTPUT.PUT_LINE ('Completed A .SQL' ); SET SERVEROUTPUT OFF; Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 8.3 Formatting Query Output in SQL* Plus 153 The same effect can be obtained by hard-coding the path name into the script using only a single @ character command as shown following Obviously, calling B .SQL with a single @ command... The && tells SQL* Plus to use the same value of the variable it already prompted for Note: Use SQL* Plus instead of SQL* Plus Worksheet when using variables because you will be able to respond to prompts from SQL* Plus more easily It is possible, but not usually practical, to use variables with SQL* Plus Worksheet iSQL*Plus does not support variables yet Please purchase PDF Split-Merge on www.verypdf.com to . you adjust for your Oracle Database 10 g session and can only be used with SQL* Plus tools (i.e., SQL* Plus, SQL* Plus Worksheet, and iSQL*Plus). Look at the. using SQL* Plus, particularly with respect to formatting. Further SQL* Plus output formatting detail is essential to proper use and understanding of Oracle SQL.

Ngày đăng: 24/12/2013, 12:17

Từ khóa liên quan

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

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

Tài liệu liên quan