[ Pobierz całość w formacie PDF ]
.PROCEDURE raise_salary (emp_info EmpRec);BEGIN.END;Initializing and Referencing RecordsThe example below shows that you can initialize a record in its type definition.When you declare a record of typeTimeRec, its three fields assume an initial valueof zero.DECLARETYPE TimeRec IS RECORD (secs SMALLINT := 0,mins SMALLINT := 0,hrs SMALLINT := 0);The next example shows that you can impose theNOTNULL constraint on any field,and so prevent the assigning of nulls to that field.Fields declared asNOTNULLmustbe initialized.DECLARETYPE StockItem IS RECORD (item_no INTEGER(3) NOT NULL := 999,description VARCHAR2(50),quantity INTEGER,price REAL(7,2));.BEGIN.END;4-44 PL/SQL User s Guide and Reference Initializing and Referencing RecordsReferencing RecordsUnlike elements in a collection, which are accessed using subscripts, fields in arecord are accessed by name.To reference an individual field, use dot notation andthe following syntax:record_name.field_nameFor example, you reference fieldhire_date in recordemp_info as follows:emp_info.hire_date.When calling a function that returns a user-defined record, use the following syntaxto reference fields in the record:function_name(parameter_list).field_nameFor example, the following call to functionnth_highest_sal references the fieldsalary in recordemp_info:DECLARETYPE EmpRec IS RECORD (emp_id NUMBER(4),job_title VARCHAR2(9),salary NUMBER(7,2));middle_sal NUMBER(7,2);FUNCTION nth_highest_sal (n INTEGER) RETURN EmpRec ISemp_info EmpRec;BEGIN.RETURN emp_info; -- return recordEND;BEGINmiddle_sal := nth_highest_sal(10).salary; -- call functionWhen calling a parameterless function, use the following syntax:function_name().field_name -- note empty parameter listTo reference nested fields in a record returned by a function, use extended dotnotation.The syntax follows:function_name(parameter_list).field_name.nested_field_nameCollections and Records 4-45 Initializing and Referencing RecordsFor instance, the following call to functionitem references the nested fieldminutes in recorditem_info:DECLARETYPE TimeRec IS RECORD (minutes SMALLINT, hours SMALLINT);TYPE AgendaItem IS RECORD (priority INTEGER,subject VARCHAR2(100),duration TimeRec);FUNCTION item (n INTEGER) RETURN AgendaItem ISitem_info AgendaItem;BEGIN.RETURN item_info; -- return recordEND;BEGIN.IF item(3).duration.minutes > 30 THEN.-- call functionEND;Also, use extended dot notation to reference the attributes of an object stored in afield, as the following example shows:DECLARETYPE FlightRec IS RECORD (flight_no INTEGER,plane_id VARCHAR2(10),captain Employee, -- declare objectpassengers PassengerList, -- declare varraydepart_time TimeRec, -- declare nested recordairport_code VARCHAR2(10));flight FlightRec;.BEGIN.IF flight.captain.name =  H Rawlins THEN.END;4-46 PL/SQL User s Guide and Reference Assigning and Comparing RecordsAssigning and Comparing RecordsYou can assign the value of an expression to a specific field in a record using thefollowing syntax:record_name.field_name := expression;In the following example, you convert an employee name to upper case:emp_info.ename := UPPER(emp_info.ename);Instead of assigning values separately to each field in a record, you can assignvalues to all fields at once.This can be done in two ways.First, you can assign oneuser-defined record to another if they have the same datatype.Having fields thatmatch exactly is not enough.Consider the following example:DECLARETYPE DeptRec IS RECORD (dept_num NUMBER(2),dept_name VARCHAR2(14),location VARCHAR2(13));TYPE DeptItem IS RECORD (dept_num NUMBER(2),dept_name VARCHAR2(14),location VARCHAR2(13));dept1_info DeptRec;dept2_info DeptItem;BEGIN.dept1_info := dept2_info; -- illegal; different datatypesEND;As the next example shows, you can assign a %ROWTYPE record to a user-definedrecord if their fields match in number and order, and corresponding fields havecompatible datatypes:DECLARETYPE DeptRec IS RECORD (dept_num NUMBER(2),dept_name VARCHAR2(14),location VARCHAR2(13));dept1_info DeptRec;dept2_info dept%ROWTYPE;Collections and Records 4-47 Assigning and Comparing RecordsBEGINSELECT * INTO dept2_info FROM dept WHERE deptno = 10;dept1_info := dept2_info;.END;Second, you can use theSELECT orFETCH statement to fetch column values into arecord, as the example below shows.The columns in the select-list must appear inthe same order as the fields in your record.DECLARETYPE DeptRec IS RECORD (dept_num NUMBER(2),dept_name VARCHAR2(14),location VARCHAR2(13));dept_info DeptRec;BEGINSELECT deptno, dname, loc INTO dept_info FROM deptWHERE deptno = 20;.END;However, you cannot use theINSERT statement to insert user-defined records intoa database table.So, the following statement is illegal:INSERT INTO dept VALUES (dept_info); -- illegalAlso, you cannot assign a list of values to a record using an assignment statement.Therefore, the following syntax is illegal:record_name := (value1, value2, value3,.); -- illegalThe example below shows that you can assign one nested record to another if theyhave the same datatype.Such assignments are allowed even if the enclosing recordshave different datatypes.DECLARETYPE TimeRec IS RECORD (mins SMALLINT, hrs SMALLINT);TYPE MeetingRec IS RECORD (day DATE,time_of TimeRec, -- nested recordroom_no INTEGER(4));TYPE PartyRec IS RECORD (day DATE,time_of TimeRec, -- nested recordplace VARCHAR2(25));4-48 PL/SQL User s Guide and Reference Manipulating Recordsseminar MeetingRec;party PartyRec;BEGIN.party.time_of := seminar.time_of;END;Comparing RecordsRecords cannot be tested for nullity, equality, or inequality.For instance, thefollowingIF conditions are illegal:BEGIN.IF emp_info IS NULL THEN.-- illegalIF dept2_info > dept1_info THEN.-- illegalEND;Manipulating RecordsThe datatypeRECORDlets you collect information about the attributes of something.The information is easy to manipulate because you can refer to the collection as awhole.In the following example, you collect accounting figures from databasetablesassets andliabilities, then use ratio analysis to compare theperformance of two subsidiary companies:DECLARETYPE FiguresRec IS RECORD (cash REAL, notes REAL,.);sub1_figs FiguresRec;sub2_figs FiguresRec;FUNCTION acid_test (figs FiguresRec) RETURN REAL IS.BEGINSELECT cash, notes,.INTO sub1_figs FROM assets, liabilitiesWHERE assets.sub = 1 AND liabilities.sub = 1;SELECT cash, notes,.INTO sub2_figs FROM assets, liabilitiesWHERE assets.sub = 2 AND liabilities.sub = 2;IF acid_test(sub1_figs) > acid_test(sub2_figs) THEN.END;Notice how easy it is to pass the collected figures to the functionacid_test, whichcomputes a financial ratio [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • hanula1950.keep.pl