1. Trang chủ
  2. » Công Nghệ Thông Tin

specifying variables at runtime

28 231 0
Tài liệu đã được kiểm tra trùng lặp

Đ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

Thông tin cơ bản

Định dạng
Số trang 28
Dung lượng 181,64 KB

Nội dung

Specifying Variables at Runtime 7 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder7Ć2 Schedule: Timing Topic 35 minutes Lecture 25 minutes Practice 60 minutes Total Class Management Note: Files required for this lesson are: Demonstration: l7varno.sql, l7varyes.sql, l7expr.sql, l7dbl.sql, l7dlb2.sql, l7prompt.sql, l7param.sql Practice: None Specifying Variables at Runtime 7Ć3 Objectives You can create a command file containing a WHERE clause to restrict the rows displayed. To change the condition each time the command file is run, you use substitution variables. Substitution variables can replace values in the WHERE clause, a text string, and even a column or a table name. At the end of this lesson, you should be able to D Create a SELECT statement that prompts the user to enter a value at runtime. D Use the SQL*Plus ACCEPT command to define a variable. D Define a variable that can be automatically picked up by the SELECT statement at runtime. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder7Ć4 Class Management Note: DEMO: l7varno.sql PURPOSE: All rows are returned, no restrictions are in place. DEMO: l7varyes.sql PURPOSE: The query is case-insensitive and is restricted to a value entered by the user. 1.Execute the script. At the prompt, enter: sales Output is restricted to rows satisfying that request. 2.Execute the script again. This time, at the prompt enter: PRESIDENT Question: How does the value become case-insensitive? Answer: The UPPER function and the LIKE operator are in the WHERE clause instead of the = operator. Specifying Variables at Runtime 7Ć5 Overview The reports produced so far have not been interactive in any way. In a finished application, the user would trigger the report, and the report would run without further prompting. The range of data reported would be predetermined by the fixed WHERE clause in the SQL*Plus script file. However, SQL*Plus enables you to create reports that prompt the user to supply their own values to restrict the range of data returned. Interactive Reports To create interactive reports, you can embed substitution variables in a command file or in single SQL commands. A variable can be thought of as a container in which values are temporarily stored. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder7Ć6 Specifying Variables at Runtime 7Ć7 Substitution Variables In SQL*Plus, you can use single ampersand substitution variable to temporarily store values. You can predefine variables in SQL*Plus by using the ACCEPT or DEFINE commands. ACCEPT reads a line of user input and stores it in a variable. DEFINE creates and assigns a value to a variable. Examples of Restricted Ranges of Data D Report figures for the current quarter or specified date range only. D Report on data relevant to the user requesting the report only. D Display personnel within a given department only. Other Interactive Effects Interactive effects are not restricted to direct user interaction with the WHERE clause. The same principles can be used to achieve other goals, for example: D Dynamically altering headers and footers. D Obtaining input parameters from a file rather than from a person. D Passing values from one SQL statement to another. SQL*Plus does not support validation checks on user input. Make sure that the prompts you write for the user are simple and unambiguous. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder7Ć8 Specifying Variables at Runtime 7Ć9 Single Ampersand Substitution Variables When running a report, users often want to restrict the data returned dynamically. SQL*Plus provides this flexibility by means of user variables. Use an ampersand (&) to identify each variable in your SQL statement. You do not need to define the value of each variable. Notation Description &user_variable Indicates a variable in a SQL statement; if the variable does not exist, SQL*Plus prompts the user for a value. SQL*Plus discards a new variable once it is used. Example Create a statement to prompt the user for a department number at runtime. The report should contain each employee’s number, last name, and salary. SQL> SELECT id, last_name, salary 2 FROM s_emp 3 WHERE dept_id = &department_number; Enter value for department_number: 31 ID LAST_NAME SALARY ---------- ------------------------- ---------- 3 Nagayama 1400 11 Magee 1400 With the single ampersand, the user is prompted every time the command is executed. SET VERIFY Command To confirm the changes in the SQL statement, use the SQL*Plus SET VERIFY command. Setting SET VERIFY to ON forces SQL*Plus to display the text of a command before and after it replaces substitution variables with values. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder7Ć10 [...]... variable Abridged Syntax ACCEPT variable [datatype][FORMAT][PROMPT text][HIDE] where: variable is the name of the variable that stores the value If it does not exist, SQL*Plus creates it datatype is either NUMBER, CHAR, or DATE CHAR has a maximum length limit of 240 bytes DATE checks against a format model, and the datatype is CHAR FOR[MAT] specifies the format model, for example A10 or 9.999 PROMPT... Ampersand Substitution Variables continued Specifying Character and Date Values with Substitution Variables Remember that in a WHERE clause, date and character values must be enclosed within single quotation marks The same rule applies to the substitution variables To avoid entering the quotation marks at run time, enclose the variable in single quotation marks within the SQL statement itself Example... Substitution variables in the command line Specifying Variables at Runtime 7Ć23 7Ć24 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Practice Overview This practice gives you the opportunity to create files that can be run interactively by using substitution variables to create runtime selection criteria Practice Contents D Creating a query to display values using substitution variables. .. variables before executing a SELECT statement SQL*Plus provides two commands for defining and setting user variables: DEFINE and ACCEPT Command Description DEFINE variable = value Creates a CHAR datatype user variable and assigns a value to it DEFINE variable Displays the variable, its value, and its datatype DEFINE Displays all user variables with value and datatype ACCEPT (see syntax below) Reads... suppresses what the user enters, for example a password Note: Do not prefix the SQL*Plus substitution parameter with the ampersand (&) when referencing the substitution parameter in the ACCEPT command Class Management Note: New features in SQL*Plus 3.2 for the ACCEPT command are FORMAT, DATE, and DEFAULT Specifying Variables at Runtime 7Ć15 Class Management Note: DEMO: l7prompt.sql PURPOSE: Demonstrate an... suppresses the listing Specifying Variables at Runtime 7Ć17 7Ć18 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Defining User Variables continued Variables are defined until you either D Issue the UNDEFINE command on a variable D Exit SQL*Plus When you undefine variables, you can verify your changes with the DEFINE command When you exit SQL*Plus, variables defined during that session are lost... total At the prompt for condition, enter: payment_type = ’CASH’ 2 Run the script again At the prompt for column name, enter: date_ordered At the prompt for condition, enter: total > 300000 7Ć12 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Single Ampersand Substitution Variables continued Specifying Column Names, Expressions, and Text at Runtime Not only can you use the substitution variables. .. for column_name: date_ordered Enter value for condition: total > 300000 ID -100 109 DATE_ORDE 31-AUG-92 08-SEP-92 If you do not enter a value for the substitution variable, you will obtain an error when you execute above command Specifying Variables at Runtime 7Ć13 7Ć14 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Defining User Variables You can predefine user variables before... DEFINE command When you exit SQL*Plus, variables defined during that session are lost To define those variables for every session, modify your login.sql file so that those variables are created at startup Example Create a variable to hold the department position Display all department names that match the position in the variable SQL> DEFINE dname = sales SQL> DEFINE dname DEFINE dname SQL> SELECT... entered at the prompt by the user SQL> SELECT 2 FROM 3 WHERE id, last_name, salary s_emp title = ’&job_title’; Enter value for job_title: Stock Clerk Specifying Variables at Runtime 7Ć11 Class Management Note: DEMO: l7expr.sql PURPOSE: Show students that you can change the column names and conditions by using substitution variables Run this script twice using the following values: 1 Run the script At the . enter: date_ordered At the prompt for condition, enter: total > 300000 Specifying Variables at Runtime 7Ć13 Single Ampersand Substitution Variables. Procedure Builder7Ć14 Specifying Variables at Runtime 7Ć15 Defining User Variables You can predefine user variables before executing a SELECT statement. SQL*Plus

Ngày đăng: 26/10/2013, 23:15