Exercise Create a PL/SQL block that determines the top five highest paid employees from your Employee table.. The event that fires the trigger, the database table on which the trigger is
Trang 12 What function do I use to combine two strings together?
You use the CONCATfunction; however, you can still rely on ||to concatenate strings.
3 What function converts ‘11/28/99’to an Oracle DATE? TheTO_DATEfunction gives you this flexibility.
4 You can use the TRUNCandROUNDfunctions with what data types?
BothNUMBERandDATEinclude the ROUNDandTRUNCfunctions.
Exercises
1 Create a PL/SQL block that reads in the month of a date and displays the month in
a Roman numeral format Use a date of 06/11/67 This allows you to practice theTO_CHARfunction When printing the Roman numeral equivalent, use LTRIMto remove any spaces padded to the left of the Roman numeral If you are really ambitious, on your own you can create the same RM-type function by usingIF THEN ELSEstatements for practice from Day 4 Remember, practice helps
to solidify your knowledge through repetition and understanding.
Here is one solution:
DECLAREv_Hold_Month Number;
BEGINv_Hold_Month := TO_NUMBER(TO_CHAR(TO_DATE(‘11-JUN-67’),’MM’));
Converted to Roman Numeral VI
2 Use the TRUNCfunction on the SYSDATEto round to the nearest century.
The answer isSELECT TO_CHAR(TRUNC(SYSDATE,’CC’),’MM/DD/YYYY HH:MI:SS AM’)
“Today’s Date and Time”
from DUALThe output is similar toToday’s Date and Time -01/01/1900 12:00:00 AM
A
Trang 23 Use CONCATto link two strings together Repeat the same line by using ||instead
ofCONCAT Here is one solution:
DECLAREv_String1 VARCHAR2(60) := CONCAT(‘Connect String1 to’,
‘ String2’);
v_String2 VARCHAR2(60) := ‘Connect String1 to’ || ‘ String2’;
BEGINDBMS_OUTPUT.PUT_LINE(v_String1);
DBMS_OUTPUT.PUT_LINE(v_String2);
END;
/
Your output looks similar to
Connect String1 to String2Connect String1 to String2
4 Calculate the number of days between 01/01/97 to 03/31/97 Remember to use the
TRUNCfunction to eliminate the TIMEdependency.
The answer is
SELECT TRUNC(TO_DATE(‘03/31/97’,’MM/DD/YY’)) - TRUNC(TO_DATE(‘01/01/97’,’MM/DD/YY’)) “Days_Subtracted”
from DUAL;
Your output is
Days_Subtracted -
from DUAL;
Your output is
Years Old -30.021918
6 Calculate how many months are between 05/15/97 and 08/22/97.
The answer is
Trang 3SELECT MONTHS_BETWEEN(‘22-AUG-97’,’15-MAY-97’) “Fractional”
from DUAL;
Your output isFractional -3.2258065
7 Round the SYSDATEto the nearest century.
The answer isSELECT TO_CHAR(ROUND(SYSDATE,’CC’),’MM/DD/YYYY HH:MI:SS AM’)
“Today’s Date and Time”
from DUAL;
Your output is similar toToday’s Date and Time -01/01/2000 12:00:00 AM
8 Calculate the time in Newfoundland from Central Standard Time from 02-22-97, 05:00 AM.
Here is one solution:
SELECT TO_CHAR(NEW_TIME(TO_DATE(‘02-22-97 05:00:00 AM’,
‘MM-DD-YY HH:MI:SS AM’),
‘CST’,’NST’), ‘DD-MON-YY HH:MI:SS AM’)
“Central to Newfoundland”
from DUAL;
Your output isCentral to Newfoundland -22-FEB-97 07:30:00 AM
9 From Listing 6.22, subtract one month and explain the answer.
Two possible answers areSELECT ADD_MONTHS(TO_DATE(‘31-MAR-97’),-1) from DUAL;
SELECT ADD_MONTHS(TO_DATE(‘31-MAR-97’),-1.5) from DUAL;
The output, of course, is the end of February because February has fewer than 30 days:
ADD_MONTH -28-FEB-97
A
Trang 410 Calculate the number of days until Christmas from the last day of the month of today’s date (We don’t get paid until the end of the month!)
Here is one solution:
SELECT LAST_DAY(SYSDATE) “Last_Day”,
TO_DATE(‘25-DEC-97’) - LAST_DAY(SYSDATE) “Shopping Days”
from DUAL;
The output is similar to
Last_Day Shopping Days - -30-JUN-97 177.67266
Day 7, “Procedures, Packages, Errors, and Exceptions”
Quiz
1 What statement do you use to recompile a procedure?
You use the CREATE OR REPLACE PROCEDUREcommand to recompile a procedure.
2 How do you invoke a procedure?
You use the executecommand if you want to explicitly and manually call a dure From within a package or another PL/SQL construct, you simply list the pro- cedure name in the code, and the call to it is made automatically.
proce-3 Name at least four predefined Oracle exception errors.
There are many Oracle predefined exceptions, including no_data_found,
too_many_rows,invalid_cursor,value_error,invalid_number,zero_divide,
cursor_already_open, and login_denied.
4 How do you call a module of a package?
To call a specific procedure within a package, you use dot notation, as shown in the following example:
package_name.procedure_name
Exercises
1 Write a package specification for the functions written in previous lessons.
Additionally, include in the specification one or two of the procedures used in this lesson.
Trang 5Package specifications contain public declarations of the name of the package and its functions and procedures The following is an example and might differ slightly from your answer:
CREATE PACKAGE day_8_package_spec as
➥packagename declarationFUNCTION inv_count (qty number, part_nbr varchar2(25)) function declaration return number;
PROCEDURE pay_salary (emp_id number);
➥proceduredeclarationPROCEDURE hire_employee (emp_name, pay_date number, pay_type char));
➥proceduredeclarationEND day_8_package_spec;
2 Write an exception-handling piece of code to trap the error of receiving more rows than you expected as well as an unknown error.
One possible way to write this exception handler isexception
WHEN too_many_rows THEN code to be executed when a SELECT returns
too many rowsEND;
WHEN others THEN code to be executed when an exception is
encountered which is not the too_many_rows
Day 8, “Using SQL to Manipulate Data and Control Transactions”
Quiz
1 Name some of the database objects that you can base a variable declaration on.
PL/SQL variables can be based on database table columns, other variables, stants, and cursors.
con-2 Name at least two of the exception types discussed in this chapter.
There are many exceptions that a programmer can prepare for while coding Some
of the most common are no_data_found,too_many_rows,invalid_cursor, andwhen_others.
A
Trang 63 Do you need to list the table column names while inserting data into that table?
No If you elect to omit the column names during an insert statement, Oracle will automatically align the input data with the columns of the data The first piece of data is inserted into the first column, the second piece of data will be inserted into the second column, and so on.
4 What are the four SQL DML statements permitted in a PL/SQL block?
The four DML statements that are supported within a PL/SQL block are INSERT,
DELETE,UPDATE, and SELECT.
This is an invalid declaration because emp_rec_typemust be declared prior to this declaration A proper declaration would be
DECLARETYPE emp_rec_type IS record(id INTEGER,name VARCHAR2(35));
emp_rec emp_rec_type;
2 Legal or not legal:
DECLAREemp_last_name %type;
This is an invalid declaration The proper declaration would have to include a table and column reference such as
emp_last_name emp.l_name%type;
3 Legal or not legal:
DECLARETYPE emp_table_type is table of VARCHAR2(55);
emp_dept_table emp_table_type;
This declaration is incorrect because the INDEX BYclause is missing This tion should look like
declara-DECLARETYPE emp_table_type is table of VARCHAR2(55)INDEX BY BINARY_INTEGER;
emp_dept_table emp_table_type;
Trang 7Day 9, “Manipulating Data with Cursors”
Quiz
1 What are the cursor attributes and what is their purpose?
The implicit and explicit cursors each have four attributes, which provide useful information about the cursor These attributes are %isopen,%found,%notfound, and
%rowcount.
2 How many cursors can you use at a time?
There are no predefined limits on the number of cursors a session can have The only constraint that limits the number of cursors is the availability of memory to manage them.
3 Where is the cursor pointer when the cursor is first opened?
The cursor pointer is pointing to immediately prior to the first row when the cursor
is first opened.
4 Name the different cursor variable parameter modes and their purpose.
The cursor variable argument can have one of three different modes:
IN—The program can have read-only abilities with the parameter In other words, the cursor argument is passed only to the procedure or function.
OUT—The program can return values to the calling PL/SQL block.
IN OUT—The program can read and write to the variable.
Exercise
Create a PL/SQL block that determines the top five highest paid employees from your Employee table Be sure to incorporate the usage of the appropriate cursor attributes Print these five employees to the screen.
This exercise can be solved in several different ways Your solution can include exception handling as well as other methods of processing the data I have chosen the following method as my solution:
DECLAREc_emp_name VARCHAR2(32);
c_sal NUMBER(9,2);
CURSOR emp_cursor is cursor declarationSELECT emp_name, pay_type
from employeeORDER BY pay_rate desc; key to getting top 5 highest paid employees
A
Trang 8OPEN emp_cursor;
FETCH emp_cursorINTO c_emp_name, c_sal; fetch into variables for later useWHILE emp_cursor%rowcount<=5 and only fetch top 5 employeesemp_cursor%found be sure there is dataLOOP
DBMS_OUTPUT (c_emp_name || ‘ is paid ‘ || c_sal );
prints results to screenFETCH emp_cursor INTO c_emp_name, c_sal;
1 Name the three collection types PL/SQL supports.
The three collection types PL/SQL supports are index-by tables, nested tables, and varrays.
2 What declaration would you use to declare a variable named emp_namewith a datatype and size that exactly match the definition of the employee.emp_namecol- umn in the database?
In this case, the declaration would be emp_name employee.emp_name%type.
3 What declaration would you use to declare a record named empthat matches the definition of a row in the employeetable?
To declare empto match the employeetable, use emp employee%rowtype.
4 What method can you call on to be sure that a collection element really exists? Theexistsmethod can be used to determine whether a given element exists.
5 What must you be sure to do before you can add data to a nested table or to a varray?
Before you can do anything with a nested table or a varray, you must initialize it with the value returned by its constructor function.
Trang 93: TYPE dept_id IS TABLE OF department.dept_id%TYPE;
4: TYPE dept_name IS TABLE OF department.dept_name%TYPE;
29: END;
30: /The nested tables types are declared in lines 3–4 The corresponding variables are declared in lines 7–8 The tables are initialized by calling their constructor meth- ods in lines 12–13 Because we know that we are going to deal with only 10 elements, only one call to extend is made for each table These calls occur in lines 16–17, and they extend each table by 10 entries The loop in lines 21–24 generates 10 new departments, numbered from 1101 through 1110 The FORALL statement in lines 26–28 inserts these
10 rows into the department table.
A
INPUT
ANALYSIS
Trang 10Day 11, “Writing Database Triggers”
Quiz
1 Which data manipulation statements can support triggers?
INSERT,UPDATE, and DELETE.
2 What are the four basic parts of a trigger?
The event that fires the trigger, the database table on which the trigger is defined, the optional WHENclause, and the PL/SQL block containing the code to be executed.
3 In a trigger, what are the correlation names :OLDand:NEWused for?
:OLDis used to refer to the values in a row before it is changed :NEWis used to refer to the values after the row is changed.
4 What is the name of the system view that can be used to retrieve trigger tions?
defini-TheUSER_TRIGGERSview shows all triggers you own In addition, you might want
to look at the ALL_TRIGGERSview and the DBA_TRIGGERSview The ALL_TRIGGERS
view adds triggers that others own but which are defined on your tables If you have database administrator privileges, the DBA_TRIGGERSview lists all triggers defined in the database.
5 What is a mutating table?
A mutating table is one that is in the process of being modified by the SQL ment which fired a trigger Because the table is being changed it is not in a consis- tent state and Oracle does not allow queries against it.
state-6 Name some possible uses for triggers.
Some possible uses for triggers are enforcing a business rule, enforcing security, logging changes, replicating data, and calculating column values.
Exercises
1 Write a set of triggers to maintain the emp_nameanddept_namefields redundantly
in the emp_deptrelation so that you do not have to join the employee and ment tables just to get a simple department listing.
Trang 11depart-Here is one solution:
CREATE OR REPLACE TRIGGER emp_dept_namesBEFORE INSERT OR UPDATE OF emp_id, dept_id ON emp_deptFOR EACH ROW
DECLAREredundant_dept_name department.dept_name%TYPE;
redundant_emp_name employee.emp_name%TYPE;
BEGIN Get the employee’s nameBEGIN
SELECT emp_name INTO redundant_emp_nameFROM employee
WHERE employee.emp_id = :NEW.emp_id;
EXCEPTION the employee record may not exist
WHEN OTHERS THENredundant_emp_name := ‘’;
WHEN OTHERS THENredundant_dept_name := ‘’;
CREATE OR REPLACE TRIGGER department_emp_deptAFTER UPDATE OF dept_name ON departmentFOR EACH ROW
BEGINUPDATE emp_deptSET emp_dept.dept_name = :NEW.dept_nameWHERE emp_dept.dept_id = :NEW.dept_id;
END;
A
I NPUT /
O UTPUT
Trang 12/Trigger created.
CREATE OR REPLACE TRIGGER employee_emp_deptAFTER UPDATE OF emp_name ON employeeFOR EACH ROW
BEGINUPDATE emp_deptSET emp_dept.emp_name = :NEW.emp_nameWHERE emp_dept.emp_id = :NEW.emp_id;
END;
/Trigger created
The first trigger, emp_dept_name, handles inserts and updates on the emp_dept table itself Whenever a new record is inserted or an existing record updated, the current employee and department names are retrieved from their respective tables and stored with the emp_dept record The second trigger, department_emp_dept, ensures that any changes to a department’s name are propagated to all the related records in the emp_dept table The third trigger does the same thing for changes to employee names Writing these triggers almost leads to a mutation problem Recall the emp_dept_updtrig- ger shown in Listing 11.3 It is defined to fire only when the dept_idfield is updated In other words, it is defined as AFTER UPDATE OF dept_id ON emp_dept Removing the words OF dept_idwould cause it to fire whenever an emp_deptrecord was changed In that case, a change to a department name would fire department_emp_dept, which would issue an update against the emp_depttable That would in turn fire the
emp_dept_updtrigger, which would issue an update against the department table, which
in turn would mutate because the SQL statement that started all this was an update against that table.
2 Write the SQL statements necessary to populate the emp_nameanddept_name
fields for any existing emp_deptrecords.
This could be done as either one or two updates Here is a solution done with one
UPDATEstatement:
UPDATE emp_dept edSET emp_name = (SELECT emp_name
FROM employee eWHERE e.emp_id = ed.emp_id),dept_name = (SELECT dept_name
FROM department dWHERE d.dept_id = ed.dept_id);
ANALYSIS
Trang 13Day 12, “Using Oracle8i Objects for Oriented Programming”
Object-Quiz
1 What is the difference between a class and an object?
A class, or an object type, as it is called by Oracle, serves as the blueprint for one
or more objects It is just a design, and you might compare it to a table definition.
An object, on the other hand, represents an instance of a class You can create many objects of a given type, just as you can create many records in a table.
2 What are the allowed return values for an ORDERfunction?
The allowed return values for an ORDERfunction are 0,1, and -1 A 0value means that the two objects being compared are equal A value of -1means that the object whose method was called is less than the other object A value of 1means that the object whose method was called is greater than the other object.
3 An object table has one column for each attribute of an object, plus one additional column What is this additional column used for?
The extra column in an object table is used to store the object identifier, which
uniquely identifies that object in the database It is an Oracle-generated value, and
is automatically assigned to each object when it is first stored in the table.
4 How is an object reference different from an object?
An object reference functions similarly to a pointer in a language such as C It is used to store a reference from one object to another It is only a pointer, and in order to access the referenced object, you must use that pointer in a query to retrieve the specified object.
5 How many attributes must an object have? How many methods?
An object must have at least one attribute It does not, however, have to have any methods.
6 What datatypes are allowed for the return value of a MAPfunction?
AMAPfunction can return values only of type NUMBER,VARCHAR2, or DATE.
7 What are accessor and mutator methods?
Accessor methods are member functions that exist primarily to enable you to
retrieve specific attribute values from an object Mutator methods are member
pro-cedures that enable you to set the value of a specific attribute or set of attributes.
Using accessor and mutator methods helps insulate your code from changes to an object’s underlying implementation.
A
Trang 141 Write a stored function that creates and returns an object of type building This function should accept as parameters the building’s name, its address, and the man- ager’s employee number Have the function check the database before creating the new buildingobject, to be sure that another building with the same name does not already exist If another building with the same name does exist, the function should return null.
Here is one solution:
1: CREATE OR REPLACE FUNCTION CreateBuilding (2: This is an example of how you can work around the3: fact that you can’t write your own “constructor” for4: the building object This stored function serves5: as a psuedo-constructor Note however, that Oracle can’t6: force you to call this
7: inBldgName VARCHAR2,8: inBldgStreet VARCHAR2,9: inBldgCity VARCHAR2,10: inBldgStateAbbr VARCHAR2,11: inBldgZip VARCHAR2,12: inBldgMgr employee.emp_id%TYPE13: ) RETURN building AS
14: TheNewBldg building;
15: NoFlag integer;
16: BEGIN17: Check to see if this building already exists
18: SELECT count(*) INTO NoFlag19: FROM buildings
20: WHERE BldgName = inBldgName;
21:
22: IF NoFlag > 0 THEN23: RETURN null;
24: END IF;
25:
26: Check to see if the manager employee id is valid
27: SELECT count(*) INTO NoFlag28: FROM employee
29: WHERE emp_id = inBldgMgr;
30:
31: IF NoFlag = 0 THEN32: RETURN null;
INPUT
Trang 1540: ,inBldgCity41: ,inBldgStateAbbr42: ,inBldgZip43: ,’’) no phone number44: ,inBldgMgr);
45:
46: RETURN TheNewBldg;
47: END;
48: /Function created
49: Create some building objects50: DECLARE
51: a_building building;
52: BEGIN53: This will succeed54: a_building := CreateBuilding(‘The Red Barn’,55: ‘101 Pasture Lane’,56: ‘Mio’,’MI’,’48826’,599);
57: dbms_output.put_line(‘Created: ‘ || a_building.BldgName);
58:
59: This will fail because the building exists
60: a_building := CreateBuilding(‘East Storage Shed’,61: ‘101 Pasture Lane’,62: ‘Mio’,’MI’,’48826’,599);
63: dbms_output.put_line(‘Created: ‘ ||
64: nvl(a_building.BldgName,’Nothing’));
65:
66: This will fail because the manager does not exist
67: a_building := CreateBuilding(‘The Blue Barn’,68: ‘101 Pasture Lane’,69: ‘Mio’,’MI’,’48826’,999);
TheCreateBuildingfunction takes five arguments: a building name, street address, city, state abbreviation, and manager ID It returns an object of typebuilding The SELECTstatement in lines 18–20 first checks to see if a building with the same name already exists Then the SELECTstatement in lines 27–29 checks to be sure
A
ANALYSIS
Trang 16that the manager ID is a valid employee ID If everything checks out, the building constructor is called in lines 37–44 to actually create the buildingobject, which is then returned to the calling program (see line 46).
The PL/SQL block at the end of the listing (lines 50–74) shows the results of three attempts to create buildingobjects The first succeeds The second fails because a build- ing with the same name already exists The third also fails, but this time because the building manager ID does not represent a valid employee ID.
2 Modify the buildingobject type definition to use a MAPfunction, instead of an
ORDERfunction, for comparisons.
Here is one solution:
1: CREATE OR REPLACE TYPE building AS OBJECT (2: BldgName VARCHAR2(40),
3: BldgAddress address,4: BldgMgr INTEGER,5: MEMBER PROCEDURE ChangeMgr (NewMgr IN INTEGER),6: MAP MEMBER FUNCTION Compare
7: RETURN VARCHAR28: );
22: END;
23: END;
24: /
Type body created
This version of the buildingobject is much the same as the one you first created from Listing 12.7, except that it has a MAPfunction defined instead of an ORDER
function This MAPfunction, declared in lines 6–7 and defined in lines 18–22 of the ond segment, simply returns the building name When comparing objects of type
sec-building, Oracle will call this function and base the comparison on the values returned.
INPUT
ANALYSIS
Trang 17Day 13, “Debugging Your Code and Preventing Errors”
Quiz
1 True or False: Logic errors are easier to debug than syntax errors.
False Because the compiler doesn’t point them out, logic errors are almost always more difficult to debug than syntax errors.
2 Missing a semicolon is what type of an error?
A syntax error.
3 Provide the answer to the calculation 6 + 4 / 2 = ?.
The expression 6 + 4 / 2evaluates to 8 The division takes precedence over the addition, so it is done first.
4 True or False: Commenting code is a waste of time.
False Comments improve the readability of code and clarify the intent of the application programmer.
5 True or False: Formatting code is not necessary.
False If you don’t format your code, it will be difficult to read, increasing the lihood of making a mistake.
like-Exercise
TheDEBUGpackage in this lesson always writes debugging messages to the file nameddebug.txt That will cause problems if multiple developers use the package
at once Modify the DEBUGpackage as follows:
• Modify the ERASEprocedure to accept a filename This filename will be used
by subsequent calls to the OUTprocedure.
• Modify both the OUTandERASEprocedures so that if no filename is passed,
or if ERASEis never called, no file is created and no messages get written.
For extra credit, add an ASSERTprocedure to the DEBUGpackage, and build in a flag
so that you can enable and disable assertions at will.
In one possible solution to the exercise, the modified DEBUGpackage would look like this:
A
Trang 181: CREATE OR REPLACE PACKAGE DEBUG AS2: /* Procedure OUT is used to output a comment of your3: choice, along with the contents of the variable The4: Procedure OUT statement defines the function.*/
5: PROCEDURE OUT(p_Comments IN VARCHAR2, 6: p_Variable IN VARCHAR2);
7:
8: /* Procedure Erase begins a new file
9: Used to start a new debugging process Good idea to call10: this function first */
11: PROCEDURE Erase (p_filename IN VARCHAR2);
12:
13: /*Procedure ASSERT tests a condition, and raises an error14: if that condition is not true */
15: PROCEDURE assert (16: p_condition IN BOOLEAN,17: p_message IN VARCHAR2);
18: END DEBUG; End Definition of package DEBUG19: /
28: BEGIN29: /* Exit if no filename has been specified */
30: IF log_filename = ‘’ THEN31: RETURN;
32: END IF;
33:
34: /* Use A to append all output being sent to the file */35: v_MyFHOUT := UTL_FILE.FOPEN(‘c:\a’,log_filename,’a’);36:
37: /* This outputs the Time and Date as MM-DD-YY HH:MM:SS38: followed by comments, and then by the contents of the39: variables Each element is surrounded by quotation marks,40: and separated by a comma.*/
41:
42: UTL_FILE.PUT_LINE(v_MyFHOUT,’”’||
43: TO_CHAR(SYSDATE,’mm-dd-yy HH:MM:SS AM’)44: || ‘“,”Comment: ‘ || p_Comments ||
45: ‘“,”Variable Contents: ‘ || p_Variable || ‘“‘);46:
47: /* Close the file handle which points to debug.txt */48: UTL_FILE.FCLOSE(v_MyFHOUT);
49: EXCEPTION50: /* Create Exception to display error code and message */51: WHEN OTHERS THEN
INPUT
Trang 1952: DBMS_OUTPUT.PUT_LINE53: (‘ERROR ‘ || to_char(SQLCODE) || SQLERRM);
54: NULL; Do Nothing55: END OUT; End Execution of Procedure OUT56:
57:
58: PROCEDURE Erase (p_filename IN VARCHAR2) IS59: v_MyFH UTL_FILE.FILE_TYPE; Create File Handle60: BEGIN
61: /* Save the filename, then check to see if it is blank
62: If the filename is blank, then do not do anything more */
63: log_filename := p_filename;
64: IF log_filename = ‘’ THEN65: RETURN;
66: END IF;
67:
68: /* Open file to overwrite current file contents
69: This erases the original file.*/
79: DBMS_OUTPUT.PUT_LINE80: (‘ERROR ‘ || to_char(SQLCODE) || SQLERRM);
81: NULL;
82: END Erase; End Procedure Erase83:
84: PROCEDURE ASSERT (85: p_condition IN BOOLEAN,86: p_message IN VARCHAR2) AS87: BEGIN
88: IF NOT p_condition THEN89: RAISE_APPLICATION_ERROR (-20000,p_message);
90: END IF;
91: END ASSERT;
92:
93: BEGIN94: Erase(‘debug.txt’); Erase contents of the file95: END DEBUG; End procedure DEBUG
96: /
As you can see, the ASSERTprocedure has been added to this package The dure header is in lines 15–17, and the procedure body is in lines 84–91 Further, theERASEprocedure has been modified to accept a filename as an argument (lines
proce-11 and 58) If the filename is blank, the ERASEprocedure stores it and quits (lines
A
Trang 2063–66) The OUTprocedure has also been modified The first thing it does, in lines 29–31, is check the filename If the filename is blank,OUTreturns without logging any debugging information So not only can you now write debugging information
to any file you like, but you can also turn the feature off.
Day 14, “Leveraging Large Object Types”
Quiz
1 What are the two types of internal LOBs?
The two types of internal LOBs are the persistent and the temporary LOBs.
2 What is the maximum size of a LOB? 4GB.
3 Can you write to external files?
Currently, you can only read from external files, not write to them.
4 When copying LOBs from one row to another, is a new locator copied?
Not only is a new locator created, but the entire LOBfrom the row is copied If you have some 4GB objects, this table can eat up storage space fast!
FALSE,DBMS_LOB.CALL);
End;
In this solution, you have created a temporary LOB of the BLOB datatype You have specified that it will not be stored in memory and will disappear after the current Oracle8i call.
Day 15, “Managing Transactions and Locks” Quiz
1 How is a transaction ended?
A transaction is ended when it is committed or rolled back.
INPUT
ANALYSIS
Trang 212 What is the difference between row locking and table locking?
Row locks are enabled when a specific row is being modified by a DML statement.
Table locks are acquired either explicitly or implicitly when either a row or a table
is being modified.
3 What is the purpose of a savepoint?
Savepoints are like bookmarks within a transaction: They facilitate the rollback of
a transaction to some intermediate point, which is defined by the placement of the savepoint.
4 What is the purpose of the two-phase commit?
Oracle’s two-phase commit mechanism guarantees that all database servers pating in a distributed transaction either all commit or all roll back the statements
partici-in the transaction.
5 What is the typical return value for when the DBMS_LOCKfunction ran successfully?
The typical returned value from the DBMS_LOCKfunction will be 0 when it ran cessfully.
suc-Exercise
Write a PL/SQL block that establishes a savepoint, inserts a single record into theemployeetable, commits the data if the new record does not replicate an existing record, or rolls back the data if the new record insert fails.
Here is one solution:
SAVEPOINT exercise; use this to roll back toINSERT INTO employee VALUES (10, ‘Loraine Williams’,2,4000.00,’S’);
COMMIT; saves the data if insert was successful
this is not executed if there is an exceptionEXCEPTION
WHEN DUP_VAL_ON_INDEX THEN exception handlerROLLBACK; back out data from insert
Day 16, “Generating Dynamic SQL”
Trang 222 What is meant by the term dynamic SQL?
A dynamic SQL statement is one that a program generates when it is running It is dynamic because it can change from one run to the next A static SQL statement,
on the other hand, is written directly into your program code, and never changes.
3 What is Oracle’s term for the new version of dynamic SQL?
Oracle uses the term native dynamic SQL to describe the new dynamic SQL
fea-tures released with Oracle8i.
4 When using native dynamic SQL, what new form of the OPENstatement is used to open a cursor on a dynamic SQL statement?
TheOPEN FORstatement is used to open a cursor on a dynamic SQL statement.
Exercise
Write a stored procedure to take a username as an argument, and create a version
ofmytablein that user’s schema.
The following is one possible solution to this problem:
1: CREATE OR REPLACE PROCEDURE make_mytable (2: username IN VARCHAR2
3: ) AS4: create_stmt VARCHAR2(200);
5: BEGIN6: Build up the CREATE TABLE statement
7: create_stmt := ‘CREATE TABLE ‘8: || username || ‘.mytable’
9: || ‘(myrow NUMBER, mydesc VARCHAR2(50))’;
10:
11: Execute the statement just built
12: EXECUTE IMMEDIATE create_stmt;
INPUT
ANALYSIS
Trang 23Day 17, “Writing to Files and the Display”
The default maximum line size is 1,023 bytes.
3 What is the maximum line size that you can specify with the FOPENcall?
The maximum line size you can specify in an FOPENcall is 32,767 bytes.
4 What does FCLOSE_ALLdo to the file open flag referenced by IS_OPEN? Nothing If you close files by using FCLOSE_ALL, the IS_OPENfunction still reports them as being open.
5 What is the maximum number of characters that can possibly be allocated to the buffer when using DBMS_OUTPUT?
One million The buffer size can range from 2,000 to 1,000,000.
Exercise
Listing 17.5 shows how to write comma-delimited data to a file Using GET_LINEinstead of PUT_LINE, modify that code to read the data back and display it by usingDBMS_OUTPUT.
The following is one possible solution to the exercise:
SQL> SET SERVEROUTPUT ONSQL>
6 Open the file
7 emp_data := UTL_FILE.FOPEN (‘c:\a’,’empdata.csv’,’R’);
Trang 2421 IF NOT end_of_file_flag THEN
22 Display the line for SQL*Plus users to see
61 WHEN others THEN
62 DBMS_OUTPUT.PUT_LINE (‘Some other error occurred.’);
63 UTL_FILE.FCLOSE_ALL;
64 END;
Trang 25PL/SQL procedure successfully completed.
Lines 11–27 contain the read loop Here, each line is read from the file and played by using DBMS_OUTPUT The actual read is embedded within a nested PL/SQL block (lines 12–19) because of the need to detect the end-of-file Encountering
dis-an end-of-file will result in a NO_DATA_FOUND exception being raised This is trapped
in lines 15–16, where the end-of-file flag is set to TRUE When the flag becomes true, the loop exits.
Day 18, “Managing Database Jobs”
Quiz
1 If the server goes down for two days (Monday to Tuesday), and a job with an cution of SYSDATE + 7was supposed to run when the server went down (Tuesday), will the job always run on the original day of the week (that is, run every
exe-Tuesday)?
No, because the new SYSDATEis assigned when the server is restored (Wednesday), the job will now be running every Wednesday until the job is altered or removed,
or another problem occurs.
2 Why must you use two single quotes around parameters specified in SUBMIT, when you used to need only one set of single quotes?
When Oracle parses the data and removes the quotes, any part that does not have two sets of single quotes and is a string parameter is stripped down to no single quotes and thus causes the job to fail.
3 Can you alter someone else’s job?
Only if you know the owner’s login and password and sign on as that person In other words…no!
A
ANALYSIS
OUTPUT