You input the PL/SQL block line by line. Ending the block with a period (.) on a line by itself stores the block in the SQL buffer.
You can save your PL/SQL block in a script file as follows:
SQL> SAVE <filename>
If you want to edit the file, you can use the SQL*Plus line editor. For instructions, see SQL*Plus User's Guide and Reference. After editing the file, you can save it again as follows:
SQL> SAVE <filename> REPLACE
SQL> RUN or SQL> /
When the block is finished running, you are returned to the SQL*Plus prompt. The SQL buffer is not cleared until you start inputting the next SQL statement or PL/SQL block.
CLEAR BREAKS CLEAR COLUMNS COLUMN ENAME HEADING Name TTITLE 'CLERICAL STAFF' DECLARE avg_sal NUMBER(7,2); BEGIN SELECT AVG(sal) INTO avg_sal FROM emp; IF avg_sal < 1500 THEN UPDATE emp SET sal = sal * 1.05 WHERE job = 'CLERK'; END IF; END; / SELECT ENAME, SAL FROM EMP WHERE JOB = 'CLERK';
The two CLEAR statements get rid of any settings left over from a previous report. The COLUMN statement changes the ENAME column heading to Name. The TTITLE statement specifies a title that appears at the top of each page in the report. The semicolon (;) following each SQL*Plus statement executes that statement. Likewise, the slash (/) following the PL/SQL block executes that block.
SQL> START <filename> or SQL> @<filename>
Your PL/SQL block can take advantage of the SQL*Plus substitution variable feature. Before running a script, SQL*Plus prompts for the value of any variable prefixed with an ampersand (&). In the following example, SQL*Plus prompts for the value of num:
SQL> BEGIN 2 FOR i IN 1..&num LOOP ... ... 8 END; 9 / Enter value for num:
SQL> CREATE PROCEDURE create_dept (new_name CHAR, new_loc CHAR) AS 2 BEGIN 3 INSERT INTO dept 4 VALUES (deptno_seq.NEXTVAL, new_name, new_loc); 5 END create_dept; 6 / Procedure created.
If SQL*Plus tells you that the subprogram, package, or trigger was created with compilation errors, you can view them by typing the SQL*Plus command SHOW ERRORS, as follows:
SQL> SHOW ERRORS
To declare a bind variable, you use the SQL*Plus command VARIABLE. In the following example, you declare a variable of type NUMBER:
VARIABLE return_code NUMBER
Note: If you declare a bind variable with the same name as a PL/SQL program variable, the latter takes precedence.
To reference a bind variable in PL/SQL, you must prefix its name with a colon(:), as the following example shows:
:return_code := 0; IF credit_check_ok(acct_no) THEN :return_code := 1; END IF;
To display the value of a bind variable in SQL*Plus, you use the PRINT command, as follows:
SQL> PRINT return_code RETURN_CODE ----------- 1
In the script below, you declare a bind variable of type REFCURSOR. (The SQL*Plus datatype REFCURSOR lets you declare cursor variables, which you can use to return query results from stored subprograms.) You use the SQL*Plus command SET AUTOPRINT ON to display the query results automatically.
CREATE PACKAGE emp_data AS TYPE EmpRecTyp IS RECORD ( emp_id NUMBER(4), emp_name CHAR(10), job_title CHAR(9), dept_name CHAR(14), dept_loc CHAR(13)); TYPE EmpCurTyp IS REF CURSOR RETURN EmpRecTyp; PROCEDURE get_staff (dept_no IN NUMBER, emp_cv IN OUT EmpCurTyp); END; / CREATE PACKAGE BODY emp_data AS PROCEDURE get_staff (dept_no IN NUMBER, emp_cv IN OUT EmpCurTyp) IS BEGIN OPEN emp_cv FOR SELECT empno, ename, job, dname, loc FROM emp, dept WHERE emp.deptno = dept_no AND emp.deptno = dept.deptno ORDER BY empno; END; END; / COLUMN EMPNO HEADING Number COLUMN ENAME HEADING Name COLUMN JOB HEADING JobTitle COLUMN DNAME HEADING Department COLUMN LOC HEADING Location SET AUTOPRINT ON VARIABLE cv REFCURSOR EXECUTE emp_data.get_staff(20, :cv)
SQL> EXECUTE create_dept('ADVERTISING', 'NEW YORK')
This call is equivalent to the following call issued from an anonymous PL/SQL block:
SQL> BEGIN create_dept('ADVERTISING', 'NEW YORK'); END;
In the next example, you use the database link newyork to call the remote stored procedure raise_salary:
SQL> EXECUTE raise_salary@newyork(7499, 1500)
You can create synonyms to provide location transparency for remote standalone procedures.
CREATE PROCEDURE calc_payroll (payroll IN OUT REAL) AS CURSOR c1 IS SELECT sal,comm FROM emp; BEGIN payroll := 0; FOR c1rec IN c1 LOOP c1rec.comm := NVL(c1rec.comm, 0); payroll := payroll + c1rec.sal + c1rec.comm; END LOOP; /* Display debug info. */ dbms_output.put_line('payroll: ' || TO_CHAR(payroll)); END calc_payroll;
When you issue the following commands, SQL*Plus displays the value of payroll calculated by the procedure:
SQL> SET SERVEROUTPUT ON SQL> VARIABLE num NUMBER SQL> EXECUTE calc_payroll(:num)
For more information about package DBMS_OUTPUT, see Oracle7 Server Application Developer's Guide.